• I am wondering if someone can help me?I would like to make a list of inventory in stock in woocommerce. I found a bit of code that almost works. It shows me a complete list of the products and the quantity but I don’t want my list to contain products with a stock quantity of 0, only the products in stock.

    I have tried a few different things but can’t seem to figure out how to do this. This code is a few years old so if I need to change anything to make it more up-to-date let me know. I did work out how to show the variants names instead of the variant ID’s.

    Here is the relevant bit of code:

    <?php
        $args = array(
    		'post_type'		=> 'product_variation',
    		'post_status' 		=> 'publish',
    	        'posts_per_page' 	=> -1,
    	        'orderby'		=> 'stock',
    	        'order'			=> 'ASC',
    		'meta_query' => array(
    				array(
    					'key' 	=> '_stock',
    					'value' => array('', false, null),
    					'compare' => 'NOT IN'
    				)
    			),
    		);
    
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
            $product = new WC_Product_Variation( $loop->post->ID );
    			?>
    <tr>
    <td><?php echo $product->get_title(); ?></td>
    <td><?php echo wc_get_formatted_variation( $product->get_variation_attributes(), true ); ?></td>
    <td><?php echo $product->sku; ?></td>
    <td><?php echo $product->get_stock_quantity(); ?></td>
    </tr>
    		<?php
    		endwhile;
    		?>

    `
    The code above is for products with variants, I also have a bit that shows the simple products in a separate table but it also shows products with a stock count of 0.

    Any Help would be greatly appreciated.

    Thanks in advance

    If this is not the right place for this post please let me know where I should put it.

  • The topic ‘List of in stock inventory’ is closed to new replies.