Menu Close

WooCommerce: View Stock History @ Product Admin

default

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>';	
	}
	
}
View Source
Posted in WooCommerce Tips