我正在为客户开发一些定制的Woocommerce功能。他们使用BACS支付网关处理人工付款。
然而,网关目前为了我们的需要而过早地减少库存,即,当订单是“暂停”时。当订单标记为“处理”或“完成”时,我只想减少库存(避免重复减少)。
我已设法阻止股票在“保持”时使用以下代码段自行减少:
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) ) {
$reduce_stock = false;
}
return $reduce_stock;
}
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
我不太确定如何继续。虽然上面的代码有效,但添加以下条件不会:
else if ( $order->has_status( 'processing' ) || $order->has_status( 'completed' ) ) {
$reduce_stock = true;
}
简而言之,我最好根据以下库存状态来改变库存:
任何帮助深表感谢!
使用连接在woocommerce_order_status_changed
的自定义函数,您将能够定位“处理”和“已完成”订单状态更改减少订单商品库存。
我已在您的功能中添加了一个条件,仅在订单上定位“BACS”支付网关。
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'bacs' ) {
$reduce_stock = false;
}
return $reduce_stock;
}
add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
// Only for 'processing' and 'completed' order statuses change
if ( $new_status == 'processing' || $new_status == 'completed' ){
$stock_reduced = get_post_meta( $order_id, '_order_stock_reduced', true );
if( empty($stock_reduced) && $order->get_payment_method() == 'bacs' ){
wc_reduce_stock_levels($order_id);
}
}
}
代码位于活动子主题(或活动主题)的function.php文件中。
经过测试和工作