skip to Main Content

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


  1. 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 like 100000) 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 (like 100000), 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:

    1. Use Stock Adjustment Instead of Setting Absolute Stock:
      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:

    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'){
         
        // Fetch the product SKU or any necessary logic before this
        $product_sku = '';  // Replace with your method to fetch SKU
        $product_id = wc_get_product_id_by_sku( $product_sku );
        $product = wc_get_product($product_id);
        
        // Ensure that stock management is enabled
        update_post_meta($product_id, '_manage_stock', 'yes');
    
        // Instead of setting an absolute stock value, try adjusting it
        $stock_adjustment = 10;  // Increase by 10 units as an example
        wc_update_product_stock($product, $stock_adjustment, 'increase');  // Use 'increase' or 'decrease'
        // 'increase' will add to the existing stock
        // 'decrease' will subtract from the existing stock
    
       }
    }
    

    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.

    1. Let WooCommerce Handle Stock Updates Automatically:
      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:

    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'){
         
        // Fetch the product SKU
        $product_sku = '';  // Replace with your method to fetch SKU
        $product_id = wc_get_product_id_by_sku( $product_sku );
        
        // Ensure that stock management is enabled for this product
        update_post_meta($product_id, '_manage_stock', 'yes');
    
       }
    }
    

    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!

    Login or Signup to reply.
  2. You could try to use instead the WC_Abstract_Product setter methods set_manage_stock() and set_stock_quantity() in your code like:

    ## Previous lines code to fetch the $product_sku
    
    // Fetch the product SKU
    $product_sku = '';  // Replace with your method to fetch SKU
    $product_id  = wc_get_product_id_by_sku( $product_sku );
    $product     = wc_get_product( $product_id );
    
    if ( is_a( $product, 'WC_Product' ) ) {
        $product_stock = 100000;
    
        $product->set_manage_stock( $true );
        $product->set_stock_quantity( $product_stock );
        $product->save();
    }
    

    It could solve your issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search