問題描述
我在 WooCommerce 上使用這個代碼的小和平從這個答案自動完成支付處理訂單:
I am using on WooCommerce this little peace of code from this answer to autocomplete paid processing orders:
/**
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
return;
}
// "completed" updated status for paid Orders with all others payment methods
else {
$order->update_status( 'completed' );
}
}
但問題是我通過 SMS 使用了一個特殊的支付網(wǎng)關(guān),該網(wǎng)關(guān)的 API 橋接在cod"付款方式上,并且訂單有時會在此woocommerce_thankyou"掛鉤上處于暫停狀態(tài).
But the problem is that I use a special payment gateway by SMS which API is bridged on 'cod' payment method, and the orders stay sometimes in on-hold status on this 'woocommerce_thankyou' hook.
所以我需要一直掃描正在處理"的訂單,以完整狀態(tài)傳遞它們.我嘗試了不同的東西和鉤子,但我無法按預(yù)期工作.
So I will need to scan all the time the 'processing' orders to pass them in complete status. I have tried different things and hooks, but I cant get it work as expected.
我該怎么做?
謝謝
推薦答案
要使此工作正常運行,您只需要一個小功能即可掃描所有帶有處理"命令的訂單.'init' 鉤子上的狀態(tài),并且會將此狀態(tài)更新為已完成".
To get this working you just need a little function that will scan all orders with a "processing" status on the 'init' hook, and that will update this status to "completed".
代碼如下:
function auto_update_orders_status_from_processing_to_completed(){
// Get all current "processing" customer orders
$processing_orders = wc_get_orders( $args = array(
'numberposts' => -1,
'post_status' => 'wc-processing',
) );
if(!empty($processing_orders))
foreach($processing_orders as $order)
$order->update_status( 'completed' );
}
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
此代碼已經(jīng)過測試且有效.
This code is tested and works.
代碼位于活動子主題(或主題)的 function.php 文件中.或者也可以在任何插件 php 文件中.
建議和更新
電子郵件通知發(fā)送兩次有一個小錯誤,在這里解決:
避免在某些自動完成的訂單上重復(fù)發(fā)送電子郵件通知
這篇關(guān)于WooCommerce 中所有現(xiàn)有處理訂單的自動完成狀態(tài)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!