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

If you have an online store with WooCommerce surely sooner or later you will need to systematically add additional text before the prices, for whatever reason, but especially if you want to highlight the prices .

Text before product price (all products)

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

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

 / * Price prefix * /
add_filter ('woocommerce_get_price_html', 'help wp_prefix_prices');
function help wp_prefix_prices ($ price) {
$ text_to_add_before_price = ' EXCLUSIVE PRICES ';
return $ text_to_add_before_price. $ price;
}

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

Text before the product price (only on discounted products)

Now we are going to put ourselves in another situation, and that is that you want the text to only be displayed on discounted products, on sale . In this case the code would be this other:

 / * Prefix for discounted prices * /
add_filter ('woocommerce_get_price_html', 'help wp_prefix_discounted_prices', 100, 2);
function help wp_prefix_discounted_prices ($ price, $ product) {
if ($ product-> is_on_sale ()) {
$ text_to_add_before_price = str_replace ('', ' 
REDUCED ', $ price); return $ text_to_add_before_price; } else { return $ price; } }

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

How do I add these codes?

To know how to add the codes, check this simple guide:

How and where to paste PHP, JS, CSS codes and functions that you find out there in WordPress

( 8 votes, average: 5 ) Rate this article to help improve the quality of the blog
Share on Twitter Share on Facebook Share on Pocket Share on LinkedIn Share on WhatsApp Share on Telegram

Did you like this article? You can’t imagine what you’re missing on YouTube !

About Us

We are BE OF THEM, a team specialized in the field of digital marketing and programming, our headquarters is in Germany, and our activity has expanded to reach all parts of the Middle East, especially the Arab Gulf countries.

Do you need to raise your site's score?

We have the perfect solution for marketing your business

Contact Us

Call us, or message us by email & whatsapp

We will be happy to talk to you, and knowing everything about your work.

All rights reserved, © 2021