44
When an order that has previously reduced stock is “cancelled” or goes to “pending payment” because the payment failed or the store admin decided to change the order status, the wc_maybe_increase_stock_levels() function triggers and restores the order items’ stock quantity.
As usual, some WooCommerce entrepreneurs asked for a way to disable this automatic restocking given their custom setup. It often goes like that – you can’t really please everyone.
This is unless you’re a smart developer and can account for both options, thanks to a WordPress “filter“. Here’s a PHP one-liner that can immediately disable this default behavior, so that you can avoid the automatic restocking (and maybe doing it manually based on your business rules). Enjoy!
PHP Snippet: Disable Automatic Restocking When A WooCommerce Order Is Set to Pending / Cancelled
/**
* @snippet Disable Restocking If WooCommerce Order Cancelled
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 7
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_can_restore_order_stock', '__return_false' );