Looking to display a custom message about limited stock in a product? This code snippet can help you.
function ts_custom_message_for_limited_stock_by_id( $title, $id ) { // Set the threshold for limited stock $stock_threshold = 5; // Check if the product is in the specified IDs and has limited stock $limited_stock_products = array( 35 ); // Add your product IDs here if ( in_array( $id, $limited_stock_products ) ) { $product = wc_get_product( $id ); // Check if the product stock is below the threshold if ( $product->get_stock_quantity() !== '' && $product->get_stock_quantity() <= $stock_threshold ) { $title .= '-<br> Hurry up!! Only 1 quantity left in stock'; } } return $title; } add_filter( 'the_title', 'ts_custom_message_for_limited_stock_by_id', 10, 2 );
Output
The below output shows that the custom message is displayed along with the product title on the WooCommerce single product page.
You can also display a custom delivery message for WooCommerce backordered products. This can help display delivery timeframes or any other relevant information specific to the customer’s location.