@anoni
Hi Ana,
I have solved this by restoring the stock levels of the order IF the order transitions from the “pending” status to the “quote” status (as it does when you manually create an order/quote in the admin panel).
This assumes you are letting WooCom manage your stock – as woocom will automatically reduce stock when you save the order, this will restore the stock levels as it transitions from the Pending -> Quote status.
It will also reduce the stock levels if you then transition the Quote status to invoice. You can adjust the logic as needed….
Add this to your child-theme’s functions.php file to test:
add_action('woocommerce_order_status_changed', 'inet_order_status_changed', 10, 4);
function inet_order_status_changed($order_id, $status_transition_from, $status_transition_to, $order) {
//If transitioning from pending to quote, woocom already subtracted stock, restore it...
if ($status_transition_from == "pending" && $status_transition_to == "quote") {
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$qty = $item->get_quantity(); // Get the item quantity
wc_update_product_stock($product, $qty, 'increase');
$order->add_order_note( 'Stock levels restored for Quote.' );
}
}
//If transitioning from quote to invoice, we restored the stock using the function above - so now we have to reduce it again!
if ($status_transition_from == "quote" && $status_transition_to == "invoice") {
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$qty = $item->get_quantity(); // Get the item quantity
wc_update_product_stock($product, $qty, 'decrease');
$order->add_order_note( 'Stock levels reduced for Quote->Invoice transition.' );
}
}
}
-
This reply was modified 4 years, 2 months ago by
Chad Reitsma.