Home » WooCommerce: View Stock History @ Product Admin

WooCommerce: View Stock History @ Product Admin

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 7
 * @donate $9     https://www.tutoraspire.com
 */

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

function tutoraspire_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', 'tutoraspire_product_meta_box' );

function tutoraspire_product_meta_box() {
    add_meta_box( 'stock_history', 'Stock History', 'tutoraspire_display_stock_history', 'product', 'advanced', 'high' );
}

function tutoraspire_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 '

' . $product->get_name() . '

'; $stock_history = get_post_meta( $product_id, '_stock_history', true ); if ( $stock_history ) { foreach ( $stock_history as $timestamp => $stockvalue ) { if ( ! $stockvalue ) continue; echo '

' . date( DATE_COOKIE, $timestamp ) . ': ' . $stockvalue . '

'; } }; echo '

Current Stock: ' . $product->get_stock_quantity() . '

'; } }

You may also like