最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - Woocommerce Disabling Auto Restocking for Cancelled Orders

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Apr 16, 2020 at 11:36 exspetexspet 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 0

I've received my answer from Woocommerce support. It's working like a charm:

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');    
}
发布评论

评论列表(0)

  1. 暂无评论