Created
December 20, 2024 05:27
-
-
Save shameemreza/20ac78e4e953d788aec9f12cc4f11ace to your computer and use it in GitHub Desktop.
Automatically Restock on Subscription Cancellation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('woocommerce_subscription_status_cancelled', 'restock_inventory_on_subscripadd_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(); | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated a bit to make the code compatible with both variable and simple subscriptions: