Displaying Recently Restocked WooCommerce Products

In a recent Business Bloomer Club discussion, a member sought a way to highlight products that have recently returned to stock. When popular items come back, they often go unnoticed by customers unless they’re already on a wishlist.

The ideal solution would be to showcase restocked products using WooCommerce’s [products] shortcode, but WooCommerce doesn’t natively track or display “back-in-stock” items. This article explores how to achieve a restock display functionality with hooks, custom fields, and product queries.

Step 1: Capture Restock Events with WooCommerce Hooks

To identify when a product is restocked, you’ll need to listen for stock updates. WooCommerce has hooks such as woocommerce_product_set_stock that trigger when stock levels change. By checking if the stock status shifts from “out of stock” to “in stock,” you can record the restock date in a custom field.

Code Example

Here’s a basic snippet to update a custom field when a product is restocked:

add_action( 'woocommerce_product_set_stock_status', 'record_restock_date', 10, 3 );
function record_restock_date( $product_id, $stock_status, $product ) {
    if ( 'instock' === $stock_status ) {
        update_post_meta( $product_id, '_restock_date', current_time('mysql') );
    }
}

This code will save the date when the stock status changes to “in stock.” With the _restock_date custom field, you can then query products based on recent restocks.

Step 2: Query Recently Restocked Products

To display these products on the frontend, you can create a custom query to pull items with the _restock_date field updated within a specific timeframe, like the last 7 days. Unfortunately, the

shortcode doesn’t support filtering by custom fields, so you’ll need to create a custom loop or modify the shortcode.

Custom Query Loop

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 8,
    'meta_key' => '_restock_date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'meta_query' => array(
        array(
            'key' => '_restock_date',
            'value' => date( 'Y-m-d', strtotime( '-7 days' ) ),
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
);
$restocked_products = new WP_Query( $args );

Using this approach, you can pull the products that were recently restocked and display them on any page.

Final Thoughts

Implementing this solution allows customers to see newly restocked products, increasing visibility and potential sales. Although it requires custom code, it’s a flexible and efficient approach that brings restocked items front and center without relying solely on a wishlist.

Related content

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

Reply

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