Php 禁用将特定订单状态(处理)的库存增加到已取消订单

Php 禁用将特定订单状态(处理)的库存增加到已取消订单,php,wordpress,woocommerce,stock,Php,Wordpress,Woocommerce,Stock,当前,订单取消增加库存以更改订单状态 我想禁用增加库存,当我将订单状态从仅处理更改为取消时,它可以增加库存水平 例如: 处理-取消:不增加库存, 已发货-已取消:不增加库存, 其他状态-已取消:增加库存[默认功能], 取消-处理:减少库存[默认功能] 我试过了 这将禁用所有订单状态更改的增加库存 移除操作在此挂钩上不起作用 以下代码作为第一个选项,不用于特定订单状态更改。此外,当我们将状态从“已取消”更改为“正在处理”时,它将禁用“减少”功能 此外,我还通过woocommerce保存了过去的订单

当前,订单取消增加库存以更改订单状态

我想禁用增加库存,当我将订单状态从仅处理更改为取消时,它可以增加库存水平

例如: 处理-取消:不增加库存, 已发货-已取消:不增加库存, 其他状态-已取消:增加库存[默认功能], 取消-处理:减少库存[默认功能]

我试过了

  • 这将禁用所有订单状态更改的增加库存
  • 移除操作在此挂钩上不起作用
  • 以下代码作为第一个选项,不用于特定订单状态更改。此外,当我们将状态从“已取消”更改为“正在处理”时,它将禁用“减少”功能
  • 此外,我还通过woocommerce保存了过去的订单状态,在post meta中更改了订单状态,并在woocommerce中使用它,但它也不起作用

  • 移除操作不起作用,否则我的工作会更轻松。哪里试过提高库存水平,我哪里做错了?

    这对我来说很有效

    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' );
    }
    

    你确定你的代码被调用了吗?
    add_action( 'woocommerce_order_status_changed', 'grab_order_old_status', 10, 4 );
    function grab_order_old_status( $order_id, $status_from, $status_to, $order ) {
     if($status_from == 'processing' && $status_to == 'cancelled'){
      remove_action('woocommerce_order_status_cancelled', 'wc_maybe_increase_stock_levels');
     }
    }
    
    add_filter( 'woocommerce_can_restore_order_stock', 'ts_do_not_restock', 10, 2 );
    function ts_do_not_restock( $true, $order ){
     $stat  = $order->get_status();
      if($stat == 'cancelled'){
       $note = 'order stat is '.$stat.' so we do not updated the item stock.';
        $order->add_order_note( $note ); // To be make sure what happened.
         return false;
        }
        else
        {
         return true;
       }
    }
    
    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' );
    }