Compare at price percentage discount

Jeremy_Yap
New Member
11 0 0

Hi,

I've been trying to find a way to display the percentage discount of some of my items after using the 'Compare at price' option for each product or variant. I managed to find the code below somewhere online and it gave me the following result.

{% if product.compare_at_price_max > product.price %}(<font color="#FFA500">
{{ product.compare_at_price_min | minus: product.price | times: 100.0 | 
divided_by: product.compare_at_price_min | money_without_currency | times: 100 
| replace: '.0', ''}}%{% endif %} OFF</font> Normal Retail)

 

 

 

0 Likes
Jeremy_Yap
New Member
11 0 0

That is fine if that item/ variant had a 'Compare at price' value. However, if there was no 'Compare at price' value for that item, the following appeared at the product page.

 

 

0 Likes
Jeremy_Yap
New Member
11 0 0

I believe that there must be something wrong with the code I used. Any suggestions please on how to amend the code? The words "OFF Normal Retail)" shouldn't appear when that item does not have a 'Compare at price' value. 

Sorry for the repeated posts but I had trouble uploading the pictures earlier. Any help will be greatly appreciated. Thanks in advance!!

 

0 Likes
Alex135
Astronaut
1889 1 462

Try:

{% if product.compare_at_price_max > product.price %}(<font color="#FFA500">
{{ product.compare_at_price_min | minus: product.price | times: 100.0 | 
divided_by: product.compare_at_price_min | money_without_currency | times: 100 
| replace: '.0', ''}}% OFF</font> Normal Retail){% endif %}

 

0 Likes
Jeremy_Yap
New Member
11 0 0

Thanks so much for that Alex!! :)

0 Likes
Ryan_Amirault
New Member
1 0 0

Hey Jeremy, I'm trying to accomplish very mucht the same % off discount on our store. Can you let me know where on your theme you embedded the code? I am thinking it was on the product.liquid, but wanted to find out exactly where in there if so. Thanks for your help!

0 Likes
blueIguana
Shopify Partner
1 0 0

Maybe a few years late for you Ryan. I decided to try the code

 

In Online store / theme / edit code / product-template.liquid

As of Sep 2019 it's about line 90 in this class & if/else clause

 

<p class="product-single__prices">
{% if product.compare_at_price_max > product.price %}

 

{% else %}

0 Likes
mehrangexim
New Member
4 0 0

please share steps to implement the code

0 Likes
Akibhusen
Excursionist
196 37 68

Hi,

 

Hope you got the answer from my side. I have update

Akibhusen Shekh | Shopify Developer
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
0 Likes