Site icon Qaisar Satti's Blogs

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>';
}

 

Exit mobile version