针对特定订单状态更改禁用增加库存 [英] Disabling increase stock for specific order status changes
问题描述
当前当订单状态变为取消
时,产品库存增加.
Currently when the order status changes to cancelled
the stock of the products is increased.
当新状态为取消
时,我想禁用库存增加,但仅针对某些先前的订单状态(并非全部).
I would like to disable the stock increase when the new status is cancelled
but only for some previous order status (not all).
例如,当状态改变时:
- 从
处理
到取消
:不增加库存 - 从
发货
到取消
:不增加库存 - 从
other-status
到cancelled
:增加库存 - 从
取消
到处理
:减少库存
- From
processing
tocancelled
: Do not increase stock - From
shipped
tocancelled
: Do not increase stock - From
other-status
tocancelled
: Increase the stock - From
cancelled
toprocessing
: Decrease the stock
到目前为止,我已经尝试过:
So far I have tried this:
我用 wc_increase_stock_levels
函数尝试了 remove_action
但它对我不起作用.我哪里做错了?
I tried remove_action
with the wc_increase_stock_levels
function but it's not working for me. Where am I doing wrong?
推荐答案
更新产品库存时无法获取之前的订单状态.这是因为 wc_maybe_increase_stock_levels
函数使用 woocommerce_order_status_cancelled
在钩子之前执行的钩子:
It is impossible to get the previous order status when updating the product stock. This is because the wc_maybe_increase_stock_levels
function triggers with the woocommerce_order_status_cancelled
hook that is executed before the hooks:
- <代码>woocommerce_order_status_' .$status_transition['from'] .'_到_' .$status_transition['to']
- <代码>woocommerce_order_status_changed
从这些钩子中,您可以获得以前和新的订单状态.
您可以通过两个步骤解决此问题:
You can get around this in two steps:
- 禁用取消状态的库存增加(无论之前的订单状态如何)
- 根据一个或多个先前的订单状态设置库存增加
- Disable the stock increase for the cancelled status (regardless of what the previous order status was)
- Set the stock increase based on one or more previous order statuses
如果您只想为状态从其他状态"更改的订单启用库存增加(custom)要取消",可以使用如下代码:
In case you want to enable the stock increase only for orders that change the status from "other-status" (custom) to "cancelled", you can use the following code:
将 other-status
替换为之前的订单状态 slug (没有wc- 前缀).
Replace
other-status
with the previous order status slug (without the wc- prefix).
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.
这篇关于针对特定订单状态更改禁用增加库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!