• I’ve imported 14,000 products sucessfully. However, over 2,000 have 0 inventory but they’re still showing as In Stock. If I go to the individual product and simply click Save, it changes to Out of Stock. I am not about to pull up 2,000 products and do this though. I’m assuming this is a WooCommerce issue. I’m trying your plugin to see if I can get a 0 inventory product to show up as Out of Stock, but no luck. I see a couple other threads about this and no, I don’t have Back Order on or to Notify about it. Any suggestions?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Here you have a script, had the same problem and done it by myself… But I would be really glad if this bug could be fixed…

    $args = array(
    	'posts_per_page'   	=> -1,
    //	'offset'			=> 1100,
    	'post_type'        	=> 'product',
    	'suppress_filters' 	=> false,
    );
    $products = get_posts($args);
    foreach($products as $product) :
    	$wcPost = wc_get_product($product->ID);
    	if($wcPost->get_type() == 'simple') :
    
    		if($wcPost->get_stock_quantity() != 0) :
    			update_post_meta($wcPost->get_ID(), '_stock_status', 'instock');
    		endif;
    
    	elseif($wcPost->get_type() == 'variable') :
    		$variationsID = $wcPost->get_children();
    
    		foreach($variationsID as $variation) :
    			$varProd = wc_get_product($variation);
    			if($varProd->get_stock_quantity() != 0) :
    				update_post_meta($varProd->get_ID(), '_stock_status', 'instock');
    			endif;
    
    		endforeach;
    	endif;	
    endforeach;
    Thread Starter getyler3

    (@getyler3)

    Many thanks Bauralex! Does this go in functions.php just like that? I added it with Snippets and it broke the site with a 500 error. Just renamed the code-snippets folder name and it’s back up. Looking forward to seeing this work! Thanks again!!

    Hey getyler3, I wrapped the code above in a function and called it in my footer and the 500 error ist most probably because there are to many products and the server gets a timeout… I have just 1100 products so I set post_per_page to 100 and incremented the offset from 0 to 1100 in a step of 100

    1 call:

    
    'posts_per_page'   	=> 100,
    'offset'		=> 0,

    2.call

    'posts_per_page'   	=> 100,
    'offset'		=> 100,

    3.call

    'posts_per_page'   	=> 100,
    'offset'		=> 200,

    I know its not a perfect way to set the stock status and you have to manually increment the offset but I’ve done that once after I filled in all the stock and now I’m changing the stock through the core function of woocommerce

    function abSetStockStatus($offset) {
    $args = array(
    	'posts_per_page'   	=> -1,
    //	'offset'			=> $offset,
    	'post_type'        	=> 'product',
    	'suppress_filters' 	=> false,
    );
    $products = get_posts($args);
    foreach($products as $product) :
    	$wcPost = wc_get_product($product->ID);
    	if($wcPost->get_type() == 'simple') :
    
    		if($wcPost->get_stock_quantity() != 0) :
    			update_post_meta($wcPost->get_ID(), '_stock_status', 'instock');
    		endif;
    
    	elseif($wcPost->get_type() == 'variable') :
    		$variationsID = $wcPost->get_children();
    
    		foreach($variationsID as $variation) :
    			$varProd = wc_get_product($variation);
    			if($varProd->get_stock_quantity() != 0) :
    				update_post_meta($varProd->get_ID(), '_stock_status', 'instock');
    			endif;
    
    		endforeach;
    	endif;	
    endforeach;
    }

    and in my footer I just call abSetStockStatus(0), abSetStockStatus(100) – inside the brackets to can enter the offset

    • This reply was modified 6 years, 10 months ago by bauralex.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Product with 0 inventory does not change to “out of stock” automatically’ is closed to new replies.