我正在使用幾個插件來幫助提高預購產品的知名度。
第一個是自定義訂單狀態專業版,我用它來創建超出默認 WooCommerce 提供的其他訂單狀態。
第二個是WooCommerce 的產品預購,與默認的 WooCommerce“延期交貨”功能相比,它允許對缺貨商品的添加到購物車和跟蹤功能略有不同。
目前,當訂單被接受時,它們會短暫進入“處理”狀態,然后在確認付款后自動更新為“已完成”。
我想添加一些 PHP 代碼,這些代碼將自動識別何時購買了預購產品并更新為自定義狀態“預購”。
這是對上一篇文章的參考,該文章參考了預購功能
uj5u.com熱心網友回復:
確認付款后自動更新為“已完成”。
我不確定上述行,如何自動completed狀態,一般情況下,產品狀態轉到on-hold付款processing完成后,訂單完成應該手動完成,除非這些不是訂購的虛擬物品。
也許您有某種自定義代碼來自動訂購完整的系統。
撇開這一點不談,您必須使用woocommerce_payment_complete_order_status過濾器掛鉤,在此處查看此掛鉤的詳細資訊https://wp-kama.com/plugin/woocommerce/hook/woocommerce_payment_complete_order_status
這個鉤子為你提供了 3 個@params:
$status- 付款完成時應設定的付款狀態$order_id- 訂單編號$order- 訂單物件。
你可以在這里做什么,你可以使用這個過濾鉤子附加一個函式,你可以遍歷訂單專案并驗證是否有任何專案是預購產品,然后你可以回傳你想要設定的訂單狀態訂單,如果你沒有找到任何東西,那么你可以回傳作為鉤子引數提供的原始狀態。
這是帶有解釋的代碼片段,您必須根據需要在此片段中進行一些修改。
/**
* Set pre order status when order has any pre order items
*
* @param string $status Payment status that should be set on payment complete.
* @param int $order_id Order ID.
* @param WC_Order $order Order object.
*
* @return string
*/
function vh_sto_wc_set_pre_order_status_on_payment_complete( $status, $order_id, $order ) {
// Make a check that new status is "processing" as we don't want to do anything for virtual orders.
// If you have to perform check for virtual orders as well then you can add or check ('processing' === $status || 'completed' === $status).
if ( 'processing' === $status && ( 'on-hold' === $order->status || 'pending' === $order->status || 'failed' === $order->status ) ) {
// Define has pre order false initially.
$has_pre_order = false;
// Loop through order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object from the order item.
$product = $item->get_product();
// Get the product id.
$product_id = $product->get_id();
// Write your logic to validate if product is a pre order product or not.
// then store in $pre_order_checked as true/false.
$pre_order_checked = '';
if ( $pre_order_checked ) {
// assuming you perform a true condition test in if statement
// then set has pre order to true.
$has_pre_order = true;
// Break the loop as we got what we want.
break;
}
}
if ( $has_pre_order ) {
$status = 'YOUR DESIRED STATUS NAME';
} else {
// Autocomplete the processing order if the pre-order is unidentified.
$status = 'completed';
}
}
return $status;
}
add_action( 'woocommerce_payment_complete_order_status', 'vh_sto_wc_set_pre_order_status_on_payment_complete', 20, 3 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508629.html
