WooCommerce

Automatically Restock Subscription Products on Cancellation in WooCommerce

· Updated Jan 19, 2025 · 4 min read

Managing stock levels is critical for online stores, especially those relying on subscription-based products. By default, WooCommerce does not automatically restock inventory when a subscription is cancelled. This can lead to inventory discrepancies and frustration for store owners.

In this post, I’ll share a simple yet powerful code snippet that automatically restocks inventory for subscription products when a cancellation occurs. This solution works for both simple and variable subscription products.

If you’re also interested in automating subscription cancellations, check out my guide on automating subscription cancellations.

Why Restocking Subscription Products Matters

When a subscription is cancelled, WooCommerce doesn’t automatically adjust stock levels for the products associated with that subscription. This can cause:

To eliminate these issues, you can use the custom code snippet shared below to automatically adjust inventory when a subscription is cancelled.

Auto-Restock Subscription Products

Here’s the code to restock inventory when a subscription is cancelled:

add_action('woocommerce_subscription_status_cancelled', 'adjust_inventory_on_subscription_cancel', 10, 1);

function adjust_inventory_on_subscription_cancel($subscription) {
    if (!$subscription) return;

    foreach ($subscription->get_items() as $item) {
        $product = $item->get_product();
        $variation_id = $item->get_variation_id();

        // Ensure the product is variable and has stock management enabled
        if ($product && $product->managing_stock()) {
            $current_stock = $product->get_stock_quantity();
            $item_quantity = $item->get_quantity();

            // Adjust stock for the specific variation or parent product
            $new_stock = $current_stock + $item_quantity;

            if ($variation_id) {
                $variation = wc_get_product($variation_id);
                if ($variation && $variation->managing_stock()) {
                    $variation->set_stock_quantity($new_stock);
                    $variation->save();
                }
            } else {
                $product->set_stock_quantity($new_stock);
                $product->save();
            }
        }
    }
}

Also shared on gist: https://gist.github.com/shameemreza/20ac78e4e953d788aec9f12cc4f11ace

How It Works:

  1. Hook into Subscription Cancellation: The woocommerce_subscription_status_cancelled action triggers the function whenever a subscription is cancelled.

  2. Adjust Inventory: The function loops through all products in the subscription, determines whether they’re stock-managed, and increases the stock level accordingly.

  3. Handle Variable Products: If the product is a variable subscription, the script updates stock for the specific variation.

More information on the woocommerce_subscription_status_cancelled action can be found here: https://woocommerce.com/document/subscriptions/develop/action-reference/#subscription-status-change-actions

Automatically Restock Subscription Products on Cancellation in WooCommerce

Step-by-Step Guide to Add the Code

Here’s how you can add this snippet on your WooCommerce store:

  1. Backup Your Site: Always take a backup of your site before making changes to code.

  2. Add the Code:

  1. Test the Functionality:

Combine Automation for a Seamless Workflow

Pair this solution with my guide on automatically cancelling subscriptions with pending cancel status to create a fully automated subscription management system. By combining both solutions, you’ll ensure that subscriptions are automatically cancelled and their inventory is promptly restocked, providing a seamless experience for both you and your customers.

Real-Life Scenarios for Using This Snippet

Subscription Box Services:

When a customer cancels their subscription, the inventory for the unfulfilled items is restocked immediately, making it available for other customers.

Digital Goods with Limited Seats:

For services like online courses or webinars, restocking upon cancellation ensures that available spots are accurately displayed.

Conclusion

With this code snippet, you can eliminate the hassle of manually adjusting inventory when subscriptions are cancelled. By automating this process, you’ll save time, reduce errors, and provide a better shopping experience for your customers.

Let me know in the comments how these solutions work for your WooCommerce store!

Shameem Reza
Written by Shameem Reza

I am a Happiness Engineer at Automattic, helping merchants turn WooCommerce chaos into calm with clear solutions and simple technical breakdowns.

Enjoyed reading this?

This site stays ad-free and independent. If something here saved you time or taught you something new, a coffee goes a long way.

Buy me a coffee ☕
Keep reading