最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress - Auto cancel woocommerce orders, if new orders do not change status before 24 hours - Stack Overflow

programmeradmin11浏览0评论

I use this code to auto cancel woocommerce orders, if new orders do not change status before 24 hours. However it does not work, after 24 hours of testing new orders are not auto cancel. Looking forward to receiving help from experts. Thanks

// Create WP-Cron event to run every hour
function schedule_autocancel_wc_orders() {
    if ( ! wp_next_scheduled( 'autocancel_wc_orders_hook' ) ) {
        wp_schedule_event( time(), 'hourly', 'autocancel_wc_orders_hook' );
    }
}
add_action( 'wp', 'schedule_autocancel_wc_orders' );

// Delete WP-Cron schedule when plugin is disabled
function unschedule_autocancel_wc_orders() {
    wp_clear_scheduled_hook( 'autocancel_wc_orders_hook' );
}
register_deactivation_hook( __FILE__, 'unschedule_autocancel_wc_orders' );

// Function to automatically cancel orders
function autocancel_wc_orders() {
    $args = array(
        'limit'      => -1, // Lấy tất cả đơn đủ điều kiện
        'orderby'    => 'date',
        'order'      => 'ASC',
        'status'     => array( 'wc-pending', 'wc-ywraq-new', 'wc-ywraq-pending' ),
        'date_created' => '<' . ( time() - DAY_IN_SECONDS ) // Chỉ lấy đơn hơn 24h
    );
    $orders = wc_get_orders( $args );

    foreach ( $orders as $order ) {
        $order->update_status( 'cancelled', 'Cancelled for missing payment' );
    }
}
add_action( 'autocancel_wc_orders_hook', 'autocancel_wc_orders' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论