How to Display Custom Stock Status on Product Pages in WooCommerce

Displaying a custom stock quantity message on your WooCommerce product page can enhance the user experience and provide more detailed information about product availability. In this tutorial, we’ll walk you through how to add custom stock statuses on WooCommerce product pages programmatically.

 

To achieve this functionality, you can use WooCommerce hooks. Add the following code to your theme’s functions.php file:

add_filter( 'woocommerce_get_stock_html', 'custom_stock_status_message', 10, 2 );

function custom_stock_status_message( $html, $product ) {
    // Check if the product is in stock
    if ( $product->is_in_stock() ) {
        // Display a custom stock message for in-stock products
        return '<p class="custom-stock-status">' . __( 'Hurry! Only 5 left', 'woocommerce' ) . '</p>';
    } else {
        return '<p class="custom-stock-status">' . __( 'Out of stock', 'woocommerce' ) . '</p>';
    }
}

 

If you want to display custom stock status for certain products, modify the code like this:

if ( $product->get_id() == 123 ) { // Replace 123 with your product ID
    return '<p class="custom-stock-status">' . __( 'Limited Edition: Grab it now!', 'woocommerce' ) . '</p>';
}

 

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!