Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Stock Report. A template page snippet to (if you are logged in as admin) output a list of products/stock (which you are managing stock for) ready for printing.
<?php
/*
Template Name: Stock Report :)
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php _e('Stock Report'); ?></title>
<style>
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; }
</style>
</head>
<body>
<header>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</header>
<section>
<?php
global $woocommerce;
?>
<table cellspacing="0" cellpadding="2">
<thead>
<tr>
<th scope="col" style="text-align:left;"><?php _e('Product', 'woothemes'); ?></th>
<th scope="col" style="text-align:left;"><?php _e('SKU', 'woothemes'); ?></th>
<th scope="col" style="text-align:left;"><?php _e('Stock', 'woothemes'); ?></th>
</tr>
</thead>
<tbody>
<?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;
?>
<tr>
<td><?php echo $product->get_title(); ?></td>
<td><?php echo $product->sku; ?></td>
<td><?php echo $product->stock; ?></td>
</tr>
<?php
endwhile;
?>
</tbody>
</table>
<h2>Variations</h2>
<table cellspacing="0" cellpadding="2">
<thead>
<tr>
<th scope="col" style="text-align:left;"><?php _e('Variation', 'woothemes'); ?></th>
<th scope="col" style="text-align:left;"><?php _e('Parent', 'woothemes'); ?></th>
<th scope="col" style="text-align:left;"><?php _e('SKU', 'woothemes'); ?></th>
<th scope="col" style="text-align:left;"><?php _e('Stock', 'woothemes'); ?></th>
</tr>
</thead>
<tbody>
<?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 );
?>
<tr>
<td><?php echo $product->get_title(); ?></td>
<td><?php echo get_the_title( $loop->post->post_parent ); ?></td>
<td><?php echo $product->sku; ?></td>
<td><?php echo $product->stock; ?></td>
</tr>
<?php
endwhile;
?>
</tbody>
</table>
</body>
</html>
@daisypeel

This comment has been minimized.

Copy link

@daisypeel daisypeel commented Feb 14, 2012

For some reason, although I have 'manage stock' ticked for all my products, and probably 80% of my products also have variations, nothing populates the tables for me. Maybe this worked before the latest update as well? Please advise, thanks!

@Ramoonus

This comment has been minimized.

Copy link

@Ramoonus Ramoonus commented Feb 15, 2012

whats the update?

@rtysmith

This comment has been minimized.

Copy link

@rtysmith rtysmith commented Aug 15, 2012

any way to make this download as a csv?

@TinyGiantStudios

This comment has been minimized.

Copy link

@TinyGiantStudios TinyGiantStudios commented May 15, 2013

Is this gist still working with WC 2.0+ ?

@nelsonroque

This comment has been minimized.

Copy link

@nelsonroque nelsonroque commented Jun 11, 2014

Hey Mike!

This code is somewhat working on my site:
http://perfect-plants.com/stock-report/

It is just not outputting all products. About 20 of 129. Any help with what I should modify? Thank you!

Sincerely,
nelson

@bongoman

This comment has been minimized.

Copy link

@bongoman bongoman commented Jun 12, 2014

Not working here with WooCommerce v2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment