WooCommerce – Output a simple, printable stock/inventory report

· · ·

screen-capture-1

Another user request today to output a list of products, as well as their stock levels, to be used to compare the amount of real stock in hand with what WooCommerce thinks is in stock.

The code below can be added as a page template in your theme. Once added to your template, just create a page in WordPress admin and assign it the “Stock Report” page template.

Theres a check at the top of the page to only let admin users in, and when viewed the page will give you a simple list of products, SKU’s and stock levels which you can then print out.

I hope you find it useful 🙂

<?php
/*
Template Name: Stock Report 🙂
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>

body { background:white; color:black; width: 95%; margin: 0 auto; }
table { border: 1px solid #000; width: 100%; }
table td, table th { border: 1px solid #000; padding: 6px; }

<?php

global $woocommerce;
?>

<?php

$args = array(
	'post_type' => 'product',
	'post_status' => 'publish',
	'posts_per_page' => -1,
	'orderby' => 'title',
	'order' => 'ASC',
	'meta_query' => array(
		array(
			'key' => '_manage_stock',
			'value' => 'yes'
		)
	),
	'tax_query' => array(
		array(
			'taxonomy' => 'product_type',
			'field' => 'slug',
			'terms' => array('simple'),
			'operator' => 'IN'
		)
	)
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

global $product;
?>

get_title(); ?>
sku; ?>
stock; ?>

<?php
endwhile;

?>

Variations

<?php

$args = array(
	'post_type' => 'product_variation',
	'post_status' => 'publish',
	'posts_per_page' => -1,
	'orderby' => 'title',
	'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 );
?>

get_title(); ?>
post->post_parent ); ?>
sku; ?>
stock; ?>

<?php
endwhile;

?>