phpwordpresswoocommerceordersinventory

WooCommerce Inventory: Re-Stock when a "Pending" order is Cancelled


I have a WooCommerce Store where I’m having an issue with Stock Management.

The stock reduction is correct when the order is under PENDING status (waiting for credit card confirmation most of the time), but the items are no re-stocked if this PENDING order is set to CANCELLED, whether it is set automatically by the inventory on-hold timeout or manually from the admin panel.

However, when order status are On Hold or Processing, if the order is Cancelled, the re-stock is working.

Any ideas what could be causing this issue? Or some function to force re-stock from Pending to Cancelled Orders?

I have WP Version 6.2.2 and WooCommerce Version 8.0.1

I've searched a lot everywhere I could, but I didn't find anything useful. Maybe my search is not accurate.


Solution

  • You can always use some additional tailored code to change that fact. try the following:

    add_action('woocommerce_order_status_pending_to_cancelled', 'restore_stock_levels_on_pending_to_cancel', 10, 2);
    function restore_stock_levels_on_pending_to_cancel( $order_id, $order ) {
        // Restore stock levels
        wc_increase_stock_levels( $order_id );
    
        // Getting WC_emails objects
        $email_notifications = WC()->mailer()->get_emails();
    
        // Sending the cancelled order email
        $email_notifications['WC_Email_Cancelled_Order']->trigger( $order_id );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    Related: Send an email notification when order status change from pending to cancelled