• Resolved Palijn

    (@palijn)


    Hello,
    great plugin, thanks !
    I am trying to use the “sort by inventory” sort.
    It’s getting hairy since I have many simple products along with many compound (variationsà products.

    Q: As far as I understand, to have the sort order work, I need to manually manage and update the stock level at the product level even if I already manage the stock at the individual variations level?
    Is that right ? Or is there a way not to have to manually update the product-level stock when the variation stock is modified ?

    Example :
    Product A (simple) : stock managed, stock : 1
    Product B (compound) : stock managed, stock : 3 <=== do I really need to set these ?
    –B variation 1 : stock managed, stock : 2
    –B variation 2 : stock managed, stock : 1

    To be clear, I expect to display on the customer-facing page product B (total 3) then product A (total 1).

    Thanks for your help !
    Regards
    Thierry

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey Thierry,

    Vladimir here from SkyVerge, and I hope you are doing well.

    Yup, in this case, you would need to manually manage and update stock at the product level. The reason for this is that the WooCommerce itself will count the largest variation stock level, as the product stock.

    So if you aren’t to manage the stock at the product level, the available stock would be 2 in this case.

    Could you please let me know if there is anything else we can help you with?

    Cheers,
    Vladimir

    Thread Starter Palijn

    (@palijn)

    Hi Vladimir,

    WooCommerce itself will count the largest variation stock level,

    Woah, what a strange decision from them.
    OKay, thanks for your answer !

    Regards
    Thierry

    Hey Thierry,

    Well, in a way, it does make sense, as when your product has 2 different variations, and if you can only choose one, you’d want to let the customers know that only a certain number of products with that variation is available.

    Cheers,
    Vladimir

    Thread Starter Palijn

    (@palijn)

    Hi,
    in an attempt to give back to the community, here is code I wrote, that takes care to update a parent product stock to the sum of all its variations’ stock anytime a product is updated.
    (This allows the code to work even stock is set from third-party stock editing plugins such as Woocommerce Stock Manager which I tested.)

    //TMI
    // IF the currently edited product has variations AND does manage stock at product level
    // THEN Populate product stock as the sum of all variations stock
    
    // Hook on the "product was updated" Woocommerce hook
    add_action( 'woocommerce_update_product', 'tmi_update_stocks_on_product_save', 10, 1 );
    
    // Do our magic on the product stock level
    function tmi_update_stocks_on_product_save ( $product_id ) {
    
    //BugFu::log('tmi_update_stocks_on_product_save: '.$product_id);
    	
    	// get the product object
         $product = wc_get_product( $product_id );
    
        // product stock is not managed (manually)
    	// if it is not managed then the DB stock entry is /null/ and will not change anyway
        if (! $product->get_manage_stock()) { return; }
    	
    	// prepare final stock value
    	$total_stock_quantity = 0;
    	
        // fetch all variations of the product
         $this_product_variations_IDs = $product->get_children();
         if (! $this_product_variations_IDs ) 
         	{
         	    // if no variations, no stock
    		 	$total_stock_quantity = 0;
         	}
         else 
         	{
         	foreach ($this_product_variations_IDs as $the_variation_ID) 
         		{
         			$the_variation = wc_get_product( $the_variation_ID );
         			// increment the product stock with this variation's stock
         			$total_stock_quantity += $the_variation->get_stock_quantity();
         		}
    	 	}
         	// Finally update the product stock
    		// side-effect : error message on product save IF only variation stock was set AND product was not reloaded
    		//  because then the product stock in the form has become different from the stock in DB
    		$product->set_stock_quantity($total_stock_quantity);
    		
    		// Unregister our hook to avoid recursive infinite loop
    		remove_action( 'woocommerce_update_product', 'tmi_update_stocks_on_product_save', 10, 1 ); 
    		$product->save();
    		// Register our hook back
    		add_action( 'woocommerce_update_product', 'tmi_update_stocks_on_product_save', 10, 1 );  
    }
    //end TMI
    

    ————

    Side note : having dug somewhat into these things, I do not see anywhere where

    WooCommerce itself will count the largest variation stock level

    .
    If the “manage product at stock level” is checked, then it will return the set stock. If it is unchecked, it won’t make any attempt (that I saw) to retrieve any stock value, which corresponds to the DB entry where _stock is of null value.

    HTH
    Thierry

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sort by inventory : sort by variation stock AND simple product stocks ?’ is closed to new replies.