Home » WooCommerce: Disable Restocking @ Order Cancelled

WooCommerce: Disable Restocking @ Order Cancelled

by Tutor Aspire

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!

Based on certain WooCommerce settings (manage stock, hold stock), a processed order that is not paid within the time range is automatically set to cancelled, and stock is restored. In today’s quick tutorial, we’ll see how to disable this WooCommerce feature.

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

You may also like