![](/img/trans.png)
[英]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.