1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

0

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey @LoicTheAztec thanks for your reply! I've tested the code but stock is not increasing :( I've tried manually and also let the time reached and was automatically cancelled by Woo, still no stock restored. Any idea what might be going on? Thanks!! – Good Old Magic Aug 24 '23 at 19:09
  • It seems to be working after adding this action: `add_action( 'woocommerce_order_status_pending_to_cancelled', 'wc_increase_stock_levels' );` Not sure if this is wrong at some level, please let me know your thoughts! Thanks! – Good Old Magic Aug 24 '23 at 20:27
  • I replaced `wc_maybe_increase_stock_levels()` with `wc_increase_stock_levels()`which is the same as `add_action( 'woocommerce_order_status_pending_to_cancelled', 'wc_increase_stock_levels' );` except that my code send an email notification additionally. – LoicTheAztec Aug 24 '23 at 21:02
  • Great, thanks for all your help!! :) – Good Old Magic Aug 25 '23 at 18:01