针对特定订单状态更改禁用增加库存 [英] Disabling increase stock for specific order status changes

查看:37
本文介绍了针对特定订单状态更改禁用增加库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前当订单状态变为取消时,产品库存增加.

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-statuscancelled:增加库存
  • 取消处理:减少库存
  • From processing to cancelled: Do not increase stock
  • From shipped to cancelled: Do not increase stock
  • From other-status to cancelled: Increase the stock
  • From cancelled to processing: 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:

从这些钩子中,您可以获得以前和新的订单状态.

您可以通过两个步骤解决此问题:

You can get around this in two steps:

  1. 禁用取消状态的库存增加(无论之前的订单状态如何)
  2. 根据一个或多个先前的订单状态设置库存增加
  1. Disable the stock increase for the cancelled status (regardless of what the previous order status was)
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆