当前当订单状态更改为 cancelled
时,产品的库存会增加。
我想在新状态为 cancelled
时禁用库存增加,但仅针对某些先前的订单状态(并非全部)。
例如,当状态改变时:
processing
到cancelled
:不增加库存shipped
到cancelled
:不增加库存other-status
到cancelled
:增加库存cancelled
到processing
:减少库存到目前为止,我已经尝试过:
我用 remove_action
函数尝试了 wc_increase_stock_levels
,但它对我不起作用。我哪里做错了?
答案 0 :(得分:1)
更新产品库存时无法获取之前的订单状态。这是因为 wc_maybe_increase_stock_levels
函数使用在钩子之前执行的 woocommerce_order_status_cancelled
钩子触发:
woocommerce_order_status_' . $status_transition['from'] . '_to_' . $status_transition['to']
woocommerce_order_status_changed
从这些钩子中,您可以获得以前和新的订单状态。
您可以通过两个步骤解决此问题:
如果您只想为状态从“other-status”(custom)更改为“cancelled”的订单启用库存增加,您可以使用以下代码:
<块引用>将 other-status
替换为之前的订单状态 slug (没有
wc- 前缀)。
add_action( 'init', 'custom_stock_increase' );
function custom_stock_increase() {
// disable stock increase when order status changes to "canceled"
remove_action( 'woocommerce_order_status_cancelled', 'wc_maybe_increase_stock_levels' );
// enable stock increase when status changes from "other-status" to "canceled"
add_action( 'woocommerce_order_status_other-status_to_cancelled', 'wc_maybe_increase_stock_levels' );
}
代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。