• Resolved exspet

    (@exspet)


    When we cancel an order, Woocommerce automatically restocking the products. But we do not want auto restocking so I’ve tried this hook:

    function filter_woocommerce_can_reduce_order_stock( $true, $order ) {
            $stat  = $order->get_status();
            if($stat == 'cancelled'){ // We want only disable restocking when status is 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; // Do not restock the product.
            }else{
                $note = 'order stat is '.$stat.' so we UPDATED the item stock.';
                $order->add_order_note( $note );
                return true; // Restock the product.
            }
    }
    add_filter( 'woocommerce_can_reduce_order_stock','filter_woocommerce_can_reduce_order_stock', 10, 2 );

    Also tried this hook too:

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

    But both not worked for me, any ideas to disabling auto restocking?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support kellymetal a11n

    (@kellymetal)

    Hi there,

    It looks like your first snippet is using the hook for when stock is reduced, rather than when it’s restored.

    However, I tried your second snippet, and it worked well on my site to prevent stock from being restored when I changed the Order status to Cancelled. The stock count stayed at the “reduced” value, and the note was added to the Order:

    Order stock
    Link to image: https://d.pr/i/PZSIee

    If it’s not working on your site, then there may be some other plugin or code in the theme preventing it from working. Please try switching back to Storefront and deactivate everything aside from WooCommerce, then see if that second snippet works on your site.

    Alternatively, I tried just removing the wc_maybe_increase_stock_levels function from the woocommerce_order_status_cancelled action. By itself it didn’t run, but after I added the removal to an action that run later, then it worked and the stock was not restored:

    
    add_action( 'after_setup_theme', 'my_stop_cancelled_restock', 0 );
    function my_stop_cancelled_restock() {
        remove_action('woocommerce_order_status_cancelled', 'wc_maybe_increase_stock_levels');    
    }
    

    I hope that helps!

    Thread Starter exspet

    (@exspet)

    Thanks for your answer, remove_action is working well for me!
    Stay safe and healthy!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Disabling Auto Restocking for Cancelled Orders’ is closed to new replies.