WooCommerce: How to add a text (prefix) before the price

woo expert

If you have an online store with WooCommerce, sooner or later you might want to add additional text before the prices, especially if you want to highlight on-sale products.

Text before product price (all products)

In this case, we are going to add some text before the visible price on the product page, here:

product normal price

The solution is very simple, just add this code, customizing the text you want to be displayed:

/* Prices prefix */
add_filter( 'woocommerce_get_price_html', 'wphelp_price_prefix' );
function wphelp_price_prefix($price){
$text_to_add_before_price = ' LIMITED TIME OFFER '; 
return $text_to_add_before_price . $price ;
}Code language: PHP (php)

What we will get, in all products, would be this:

product changed price in all products

Text before the product price (only for discounted products)

Now let’s put ourselves in another situation, and that is that you want the text to be displayed only in the discounted products, on sale. In this case, the code would be this one:

/* On sale price prefix */
add_filter( 'woocommerce_get_price_html', 'wphelp_sale_price_prefix', 100, 2 );
function wphelp_sale_price_prefix( $price, $product ) {
if ( $product->is_on_sale() ) {
$text_to_add_before_price = str_replace( '<ins>', '<ins><br>ON SALE ', $price);
return $text_to_add_before_price ;
}else{
return $price;
}
}Code language: PHP (php)

The result is this, showing the prefix only on discounted products and just in front of the price on offer:

product changed price in discounted products only

Remember: Your additional text will take YOUR price’s font styles.

How do I add these codes?

To learn how to add the codes check this simple and easy guide:

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

1 thought on “WooCommerce: How to add a text (prefix) before the price”

  1. Thank you for the snippets. It’s exactly what I needed.
    Now I just need to find out how to hide regular price when the product is on sale.
    Do you maybe have a snippet for that? 🙂

Leave a Comment

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

Scroll to Top
Skip to content