WooCommerce: View Stock History @ Product Admin

When questions such as “How do you save the product stock inventory history?” pop up in our private Business Bloomer Club 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
 * @community     https://businessbloomer.com/club/
 */

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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]

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. Follow @rmelogli

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

  1. Hello,

    showcase of your snippet is showing also Date of stock movement, but for me is not showing.
    Can you tell me, where is the problem?

    Thank you in advance.

    1. Hi Jacob, can you share a screenshot please?

  2. Hello and thank you for the great snippet, 2 questions,
    1) How to add the “last 10 movements”
    2) Can I record who user did the stock changes?

    Thank you very much!

    1. Hi Dorothy, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hello Rodolfo,
    Very interesting solution.
    Is there a way to pull/display the name of the customer or shop manager after updating stock. This can go befor ar after rhe date.
    My customer needs this to track who wes behid updating the stock.
    Thanks a ot in advance!

    1. Hi Mustapha, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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