Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Automatically Restock on WooCommerce Subscription Cancellation?

In WooCommerce, managing stock levels manually can become challenging, especially when handling bulk refunds or subscription cancellations. Such refunds or cancellation of subscriptions often leads to incorrect stock quantities. To address these challenges, when processing mutiple refunds through a custom bulk action like “Refund with Restock” or subscription cancellations this code ensures inventory levels remain accurate and up to date. Below, we provide a solution that will restock items automatically during subscription cancellations.
Note: This feature is dependent on the WooCommerce Subscriptions plugin being active.

Solution: Automatically Restock on WooCommerce Subscription Cancellation

This code ensures that when a subscription is canceled in WooCommerce, the stock levels of the associated products are automatically updated. The quantities reserved for the subscription are returned to inventory.

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

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

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

        if ($product && $product->managing_stock()) {
            $current_stock = $product->get_stock_quantity();
            $item_quantity = $item->get_quantity();

            // Increase the stock by the quantity in the canceled subscription
            $new_stock = $current_stock + $item_quantity;

            $product->set_stock_quantity($new_stock);
            $product->save();
        }
    }
}

Output

When a customer cancels the subscription order from My Account >subscriptions, the code will restore the stock quantity by adding back the items from the canceled subscription to the product’s current inventory.

Similarly, the same restocking approach can be applied to WooCommerce orders as well. Keeping track of your stock can get tricky when you’re dealing with things like bulk refunds. In such cases, you can use a WooCommerce customization that allows you to process bulk refunds with or without restocking, which is a useful feature for automatically adjusting inventory after a refund.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x