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 .
Index of contents
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
YOU MAY ALSO BE INTERESTED IN …
Did you like this article? You can’t imagine what you’re missing on YouTube !