WooCommerce: View Stock History @ Product Admin

When questions such as “How do you save the product stock inventory history?” pop up in our private Bloomer Armada slack channel for WooCommerce developers, I can’t really do without thinking of coding it myself!

This neat customization saves the stock quantity of a simple product or a variable product variation before there is a stock change, due to a manual stock quantity edit or a customer order.

Please bear in mind that if you have hundreds of stock movements per product this may slow down your backend and/or database, so the snippet may need some sort of optimization or limitation (“last 10 movements”).

So, let’s see how it works. Enjoy!

The snippet below works also for variable products (as well as simple ones). The new “Stock History” meta box will show at the bottom of the Edit Product page, and if the product is variable it will give a stock movements report for each variation. Each line corresponds to a stock quantity edit, whether this is due to a manual action by the store manager or a customer order. The last line always returns the current stock value.

PHP Snippet: Display Stock Inventory Movements @ Product Edit Page

/**
 * @snippet       Save & Show Product Stock History
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_product_before_set_stock', 'bbloomer_historical_stock_product_parent' );
add_action( 'woocommerce_variation_before_set_stock', 'bbloomer_historical_stock_product_parent' );

function bbloomer_historical_stock_product_parent( $product ) {
	$stock_history = get_post_meta( $product->get_id(), '_stock_history', true ) ? get_post_meta( $product->get_id(), '_stock_history', true ) : array();
	$stock_history[time()] = (int)get_post_meta( $product->get_id(), '_stock', true );
	update_post_meta( $product->get_id(), '_stock_history', $stock_history );
}

add_action( 'add_meta_boxes', 'bbloomer_product_meta_box' );

function bbloomer_product_meta_box() {
    add_meta_box( 'stock_history', 'Stock History', 'bbloomer_display_stock_history', 'product', 'advanced', 'high' );
}

function bbloomer_display_stock_history() {
	global $post;
	$product = wc_get_product( $post->ID );

	if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $products[] = $key['variation_id'];
		}
	} else $products[] = $post->ID;
	
	foreach ( $products as $product_id ) {
		$product = wc_get_product( $product_id );
		echo '<h3>' . $product->get_name() . '</h3>';
		$stock_history = get_post_meta( $product_id, '_stock_history', true );
		if ( $stock_history ) {
			foreach ( $stock_history as $timestamp => $stockvalue ) {
				if ( ! $stockvalue ) continue;
				echo '<p>' . date( DATE_COOKIE, $timestamp ) . ': <b>' . $stockvalue . '</b></p>';
			}
		};	
		echo '<p>Current Stock: <b>' . $product->get_stock_quantity() . '</b></p>';	
	}
	
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

2 thoughts on “WooCommerce: View Stock History @ Product Admin

  1. Hello,

    Is this putting a lot of stress on wordpress?

    1. Only if you have thousands of products and a lot of stock changes, in which case this needs to be recoded and maybe use custom DB tables

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *