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, we’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:
- Inventory Mismatches: Products appear as out of stock when they’re actually available.
- Lost Sales: Customers may not purchase items marked as unavailable.
- Extra Manual Work: Store admins must manually adjust inventory levels.
To eliminate these issues, you can implement the code snippet 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:
- Hook into Subscription Cancellation: The
woocommerce_subscription_status_cancelled
action triggers the function whenever a subscription is cancelled. - Adjust Inventory: The function loops through all products in the subscription, determines whether they’re stock-managed, and increases the stock level accordingly.
- 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
Step-by-Step Guide to Add the Code
Here’s how you can implement the snippet on your WooCommerce store:
- Backup Your Site:
- Always take a backup of your site before making changes to code.
- Add the Code:
- Go to your WordPress dashboard.
- Navigate to Appearance > Theme File Editor or use a plugin like Code Snippets.
- Add the snippet to your child theme’s
functions.php
file or within the Code Snippets plugin.
- Test the Functionality:
- Create a test subscription product and purchase it as a customer.
- Cancel the subscription and verify if the stock levels adjust automatically.
Combine Automation for a Seamless Workflow
Pair this solution with our 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!
Leave a Reply