Problem: Woocommerce – Adding a Prefix ‘From: ‘ to Product Prices
Woocommerce Adding Prefix Product Prices
A customer recently challenged us to add a prefix ‘From: ‘ to product prices, but only for certain products. Woocommerce Adding Prefix Product Prices.
As with most companies, any kind of ecommerce or woocommerce in catalogue mode, the products can have lots of different variables and different ways to display certain products. At Fertile Frog we use Woocommerce as our primary platform for ecom, product and services, because it is a clean CMS for our users to maintain and because is has a strong track record of developers and updates. We however have to adapt the platform to perform differently depending on what usability and functionality our clients want.
Adding variations to the display of products can be a complicated task, when it only effects certain products and depending on where you want the variations to display.
First attempt to solve the addition of Woocommerce Price Prefixs
Firstly we used a PHP snippet from the following article that added prefix and suffix to the product prices:
Even-though this was an excellent snippet, it only displayed the prefix on the product pages. I was concerned that the usability was a little disjointed. The user would not see the ‘From ‘ prefix when browsing products on the category pages or any of the highlighted product sliders. For some clients, the display of the prefix only on the product pages could be perfect, especially if it used for the suffix to display something like ‘ plus VAT’ but we needed the prefix on all the pages that certain products was displayed on.
Creating our own snippet to add a prefix to only certain products
Start by making sure you have created a functions.php file in the child theme. Then create a function with a switch / case to run though each of the products and identify if the product ID. This will then return the prefix before the price if the ID matches the case number.
add_filter( ‘woocommerce_get_price_html’, ‘custom_price_message’, 10, 2 );
function custom_price_message( $price, $product ) {
$product_id = $product->get_id();
switch ($product_id) {
case 119257:
case 120298:
case 119261:
case 120300:
case 120301:
case 120300:
case 119258:
return ‘From: ‘ . $price ;
break;
default:
return $price ;}
}
Site referenced: rejuvamed.co.uk
Company: fertilefrog.com
Author: fertilefrog.com/staff/kt-allen/