I have this a this action to fired when an order changes status to "processsing".
This code causes a problem: the cart is no cleared after a purchase.
I found that the line which is causing this issue is: wc_update_product_stock( $product, $product_stock , 'set' );
But I don’t how to fix it. Is there another way to update the product stock? Why is this happening?
Important: only happens with orders which get the "processing" status right away in the purchase.
This issue doesn’t happen if the order is set to "on-hold" and then I edit the order and change the status manually to "processing".
add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
function so_status_completed($order_id, $old_status, $new_status){
if($new_status == 'processing' || $new_status == 'on-hold'){
// in previous lines I fetch the $product_sku
$product_id = wc_get_product_id_by_sku( $product_sku );
$product = wc_get_product($product_id);
$product_stock = 100000;
update_post_meta($product_id, '_manage_stock', 'yes');
wc_update_product_stock( $product, $product_stock , 'set' ); // THIS LINE CAUSES THE ISSUE
}// close if($new_status == 'processing'){
}
2
Answers
Hope this will help:
The issue with the cart not being cleared after a purchase is likely related to the use of
wc_update_product_stock()
in your function. This function is used to directly set the stock quantity for a product, and the way you’re setting it (to a large fixed value like100000
) is overriding WooCommerce’s normal stock management process. This could interfere with the flow of how WooCommerce handles orders, especially when moving from "on-hold" to "processing."Why It Works When You Change the Status Manually:
When an order is placed, WooCommerce automatically reduces stock for items, but when you use
wc_update_product_stock()
with a specific stock number (like100000
), it may bypass that mechanism. When you manually change the order status, WooCommerce’s internal logic kicks in differently, hence it doesn’t interfere with stock handling.Solution Options:
Instead of using
wc_update_product_stock()
with the'set'
parameter (which sets the stock to an absolute value), you can adjust the stock using the'increase'
or'decrease'
options, which might preserve WooCommerce’s stock management logic better.Here’s an example of how you can modify your function to adjust stock instead:
This method doesn’t override the entire stock value and instead adjusts it incrementally, which avoids interfering with WooCommerce’s internal stock reduction process during the purchase.
WooCommerce automatically handles stock updates when an order is placed. If your logic doesn’t require custom stock handling (like forcing a specific stock value), you can remove the line
wc_update_product_stock()
altogether and let WooCommerce manage it.If you want to ensure stock management is turned on but leave WooCommerce to handle the actual stock reductions, you could keep only the
update_post_meta()
line:By removing the direct stock update, WooCommerce’s automatic stock management system will take care of the inventory reduction when an order is placed.
Conclusion:
The issue stems from the use of
wc_update_product_stock()
with the'set'
option, which overrides WooCommerce’s internal stock handling logic. Try using'increase'
or'decrease'
instead, or remove the stock adjustment line altogether and let WooCommerce handle it automatically.Let me know if you need more details or further adjustments!
You could try to use instead the
WC_Abstract_Product
setter methodsset_manage_stock()
andset_stock_quantity()
in your code like:It could solve your issue.