Back to Top

How to Manage WooCommerce Product Stock History?

Updated 1 November 2023

In this dev blog, we will learn how to manage WooCommerce product stock history which is crucial for keeping track of inventory changes.

Also, helps online store owners to make informed decisions about restocking and sales strategies. Here’s a step-by-step guide to managing product stock history:

1. Create a custom plugin or use your theme’s functions.php file

You can develop a custom WooCommerce plugin or edit the code of your theme template files.

<?php
/**
 * Plugin Name: Woocommerce manage product stock history.
 * Description: How to manage product stock history in WooCommerce.
 * Plugin URI: https://webkul.com/
 * Version: 1.0.0
 * Author: Webkul
 * Author URI: https://webkul.com/
 * Text Domain: webkul
 */

2. Manage product stock history for simple products

add_action( 'woocommerce_product_before_set_stock', 'wkwc_historical_stock_product_parent' );

function wkwc_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 );

}

3. Manage product stock history for variable products

add_action( 'woocommerce_variation_before_set_stock', 'wkwc_historical_stock_product_parent' );

4. Add product meta box

In the code snippet, you added the wkwc_display_stock_history() function to the add_meta_boxes hook. This function’s purpose should be to add post-meta boxes.

add_action( 'add_meta_boxes', 'wkwc_product_meta_box' );

function wkwc_product_meta_box() {

	add_meta_box( 'stock_history', 'Stock History', 'wkwc_display_stock_history', 'product', 'advanced', 'high' );

}

function wkwc_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 );
		$product_stock_status = get_post_meta( $product_id, '_stock_status', true );
		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>' . gmdate( DATE_COOKIE, $timestamp ) . ': <b>' . $stockvalue . '</b></p>';

			}
		};
		$stock_quantity = ! empty( $product->get_stock_quantity() ) ? $product->get_stock_quantity() : $product_stock_status;
		echo '<p>Current Stock: <b>' . $stock_quantity . '</b></p>';
	}

}

Support

Thank you for reading this dev article to manage product stock history in WooCommerce, for any technical assistance, please reach us by email at [email protected]

Searching for an experienced
Woocommerce Company ?
Find out More

Also, you may Hire WooCommerce Developers to work on your projects.

Have a great day ahead! See you in the next post. Keep reading 🙂

. . .

Leave a Comment

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


2 comments

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home