WooCommerce禁用已取消订单的自动补货

时间:2020-04-16 作者:exspet

当我们取消订单时,Woocommerce会自动重新进货。但我们不想自动重新进货,所以我尝试了这个钩子:

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 );
也试过这个钩子:

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;
    }
}
但这两种方法对我都不起作用,有没有办法禁用自动进货?

1 个回复
最合适的回答,由SO网友:exspet 整理而成

我已经从Woocommerce支持部门收到了我的答复。它就像一个魔咒:

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