[英]Add user meta data as order meta on WooCommerce order status change
[英]WooCommerce: Change order status based on custom Meta Value
我正在尝试每天运行以下 function,以自动完成所有超过 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' );
}
我错过了什么错误吗? 谢谢你的帮助!
你做了一个很好的尝试,但你犯了一些错误。
以下代码进入您的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' );
您的 function 有小错误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
}
};
错误解释
虽然您的尝试方向正确,但'date_created' => '<'. ( time() - 10 * DAY_IN_SECONDS ),
'date_created' => '<'. ( time() - 10 * DAY_IN_SECONDS ),
你必须使用>
而不是<
并且你没有真正设置DAY_IN_SECONDS
你必须用 86400 替换它。所以正确的值是'>'. ( time() - 864000 )
'>'. ( time() - 864000 )
。 10 天10 * 86400 = 864000
。 您可以在 WooCommerce 文档中阅读此说明。
在这里,我为您创建了使用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 );
$order->save();
所以总结一下,你必须在你的函数中添加以下代码。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 );
上面的代码已经过测试并且可以工作。
安装WP CRON 插件来检查你的 cron。 见上面的截图。 您可以点击Run Now
进行测试。
警告:
当有人访问您的网站时,WP Cron 就会运行。 因此,如果没有人访问, ?> cron 永远不会运行。
这是我根据 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
已满? 请先在没有时间表的情况下测试您的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.