WooCommerce:根据自定义元值更改订单状态

时间:2021-05-25 14:04:42

标签: php wordpress woocommerce hook-woocommerce

我正在尝试每天运行以下功能,以自动完成所有超过 10 天且具有特定自定义元值的处理订单。

我正在使用以下代码段,但这根本行不通。知道为什么吗?

function autoComplete(){
    $orders = wc_get_orders( array(
            'status' => 'wc-processing',
            'date_created' => '<' . ( time() - 10 * DAY_IN_SECONDS ),
            'meta_key'     => 'status_us',
            'meta_compare' => '=',
            'meta_value'   => 'Sent to USA',
    ) );

    foreach ($orders as $order){
        $order->update_status( 'completed' );
    }
}

if ( ! wp_next_scheduled( 'autoComplete' ) ) {
    wp_schedule_event( time(), 'daily', 'autoComplete' );
} 

有什么我遗漏的错误吗?感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

你做了一个很好的尝试,但你犯了一些错误。

以下代码包含在您的 functions.php 中。

add_action( 'wp_loaded','start_custom_code' );

// Once everything theme and plugins are loaded we register our cron job.
function start_custom_code() {
    if ( ! wp_next_scheduled( 'bks_mark_processing_order_complete_if_sent_to_usa' ) ) {
        wp_schedule_event( time(), 'daily', 'bks_mark_processing_order_complete_if_sent_to_usa' );
    }
}

add_action( 'bks_mark_processing_order_complete_if_sent_to_usa', 'bks_mark_processing_order_complete_if_sent_to_usa' );

您的函数有小错误bks_mark_processing_order_complete_if_sent_to_usa()

function bks_mark_processing_order_complete_if_sent_to_usa(){
    $args = array(
        'status' => array( 'wc-processing'),
        'limit'  => -1,
        'date_created' => '>' . ( time() - 864000 ), // your mistake 1
        'status_us' => 'Sent to USA', // your mistake 2
    );


    $orders = wc_get_orders( $args );

    foreach ($orders as $order){
        $order->update_status( 'completed' );
        $order->save(); // your mistake 3
    }
};

错误解释

  1. 虽然您的尝试方向正确,但 'date_created' => '<' . ( time() - 10 * DAY_IN_SECONDS ), 您必须使用 > 而不是 <,但您也没有实际设置 DAY_IN_SECONDS 您必须用 86400 替换它。所以正确的值应该是 '>' . ( time() - 864000 )。 10 天 10 * 86400 = 864000。您可以在 WooCommerce 文档中阅读有关此解释的内容 here

  2. 在这里,我为您创建了新的自定义变量,该变量使用 woocommerce_order_data_store_cpt_get_orders_query 进行设置,然后进行查询。需要添加的代码。

function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['status_us'] ) ) {
        $query['meta_query'][] = array(
            'key' => 'status_us',
            'value' => esc_attr( $query_vars['status_us'] ),
        );
    }

    return $query;
}

add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_query_var', 10, 2 );
  1. 您更新了状态,但忘记保存。 $order->save();

总而言之,您必须在functions.php中添加以下代码

add_action( 'wp_loaded','start_custom_code' );
add_action( 'bks_mark_processing_order_complete_if_sent_to_usa', 'bks_mark_processing_order_complete_if_sent_to_usa' );


function start_custom_code() {
    if ( ! wp_next_scheduled( 'bks_mark_processing_order_complete_if_sent_to_usa' ) ) {
        wp_schedule_event( time(), 'daily', 'bks_mark_processing_order_complete_if_sent_to_usa' );
    }
}

function bks_mark_processing_order_complete_if_sent_to_usa(){
    $args = array(
        'status' => array( 'wc-processing'),
        'limit'  => -1,
        'date_created' => '>' . ( time() - 864000 ),
        'status_us' => 'Sent to USA',
    );


    $orders = wc_get_orders( $args );

    foreach ($orders as $order){
        $order->update_status( 'completed' );
        $order->save();
    }
};

function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['status_us'] ) ) {
        $query['meta_query'][] = array(
            'key' => 'status_us',
            'value' => esc_attr( $query_vars['status_us'] ),
        );
    }

    return $query;
}

add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_query_var', 10, 2 );

以上代码已经过测试并且有效。

证明: enter image description here

安装 WP CRON plugin 以检查您的 cron。见上面的截图。您可以通过点击 Run Now 进行测试。

<块引用>

警告:
当有人访问您的网站时,WP Cron 就会运行。因此,如果没有人访问, ?>cron 永远不会运行。

阅读:https://wordpress.stackexchange.com/a/179774/204925

答案 1 :(得分:0)

这是我根据 CRON 任务验证订单的操作。

$order->payment_complete('transaction ID string'); //validate payment
wc_reduce_stock_levels($order->get_id()); //reduce stock
$woocommerce->cart->empty_cart(); //empty cart
$order->update_status('completed', 'A order note string'); //change order statyus

我相信您不需要“wc-”前缀。

您确定您安排的活动有效吗?并且 $orders 已填满?请先在没有时间表的情况下测试您的方法。