我已經集成了支付網關來接受在woocommerce上運行的商店的在線支付。一切正常,但我注意到woocommerce默認情況下將所有在線付款訂單的訂單狀態更改爲wc-processing
。WooCommerce訂單狀態從支付網關變更
根據我的商店功能,我希望所有在線付款訂單最初都在wc-on-hold
狀態。
有沒有辦法阻止woocommerce以編程方式將訂單狀態更改爲wc-processing
?
我已經集成了支付網關來接受在woocommerce上運行的商店的在線支付。一切正常,但我注意到woocommerce默認情況下將所有在線付款訂單的訂單狀態更改爲wc-processing
。WooCommerce訂單狀態從支付網關變更
根據我的商店功能,我希望所有在線付款訂單最初都在wc-on-hold
狀態。
有沒有辦法阻止woocommerce以編程方式將訂單狀態更改爲wc-processing
?
是的,有一種方法,但您需要修改付款插件或添加您自己的代碼,您可以read this瞭解付款的工作方式。
現在,woocommerce使用$order->payment_complete()
方法來處理完成的訂單,所以你需要連接自己的函數來修改狀態,here is the description of that method
使用此過濾器:woocommerce_payment_complete_order_status
這是基於代碼片段在這thread。我們在這裏使用woocommerce_thankyou
(只支付已經完成之後被解僱)掛鉤我們的功能,'processing'
訂單狀態轉換爲'on-hold'
:
add_action('woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1);
function custom_woocommerce_paid_order_status($order_id) {
if (! $order_id) {
return;
}
global $woocommerce;
$order = new WC_Order($order_id);
// 'processing' orders status are converted to 'on-hold'.
if (is_object($order) && $order->has_status('processing') {
$order->update_status('on-hold');
}
return;
}
你也可以指定你的條件支付網關,例如我們繞過3個支付網關,並使用"your_payment_gateway"
slug:
add_action('woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1);
function custom_woocommerce_paid_order_status($order_id) {
if (! $order_id) {
return;
}
global $woocommerce;
$order = new WC_Order($order_id);
// Bypass orders 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;
}
// Target your "your_payment_gateway_slug" with this conditional
if (is_object($order) && get_post_meta($order->id, '_payment_method', true) == 'your_payment_gateway_slug' && $order->has_status('processing')) {
$order->update_status('on-hold');
}
return;
}
此代碼片段繼續執行您活動的子主題或主題的function.php文件。
您可以輕鬆地做任何你想要的,並支付訂單的正確鉤woocommerce_thankyou
參考文獻:
非常感謝您,但在您的解決方案中,訂單將會從「網關」轉變爲「暫停」,並已標記爲「正在處理」。我的問題是我有一個電子郵件警報流程連接到訂單狀態,其中''等待''應該從網關處理的第一狀態。 – krishna89
您說得對''order-> payment_complete()'正在將我的付款插件中的訂單狀態更改爲'處理'。我會嘗試放置我自己的功能,讓你知道。 – krishna89
而不是'$ order-> payment_complete()'我添加了下面的代碼,它按照我的預期工作。 '$命令 - > update_status( '保留'。); add_post_meta($ order-> id,'_paid_date',current_time('mysql'),true); update_post_meta($ order-> id,'_transaction_id',$ txnid); $ order-> reduce_order_stock();' – krishna89