What's your biggest current challenge? Have your say in Community Polls along the right column.

Unable to display out the percentage badge

Solved

Unable to display out the percentage badge

Boonphei
Shopify Partner
38 0 6

Hi there, I was using this code to display my product discount badge but the code was able to run on span but unable on using assign. Since I want to display the percentage like the second image.

Boonphei_1-1653965876700.png Boonphei_2-1653965922929.png

 

 

{% if product.compare_at_price >= 0 and product.compare_at_price != null %}
<div class="product-photo-discounted-price-custom">
<span>Save</span>
<!-- <span>{{ product.compare_at_price_max | minus:product.price | times:100 | divided_by:product.compare_at_price_max | json }}%</span> -->
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price %}
{% assign percentage = saving | at_least: percentage %}
</div>
{% endif %}

Accepted Solution (1)
made4Uo
Shopify Partner
3856 717 1199

This is an accepted solution.

Oh. Got it. You just have to add the forloop for variants. Try this

{% if product.compare_at_price >= 0 and product.compare_at_price != null %}
<div class="product-photo-discounted-price-custom">
      {% assign percentage = 0 %}
      {% for variant in product.variants %}
      {% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
      {% assign percentage = saving | at_least: percentage %}
      {% endfor %} 
<span>Save</span><br>
     {{ percentage | append: "%" }}
</div>
{% endif %}

 

If this fixed your issue Likes and Accept as Solution is highly appreciated. Coffee tips fuels my dedication.
Get EXPERIENCED Shopify developers at affordable rates—visit Made4Uo.com for quick quote!
Do not lost your Shopify store! Get FREE trial with ✔️ Rewind Backup: Automatic, reliable, stress-free

View solution in original post

Replies 3 (3)

made4Uo
Shopify Partner
3856 717 1199

Hi @Boonphei,

Try this code instead. 

{% if product.compare_at_price >= 0 and product.compare_at_price != null %}
<div class="product-photo-discounted-price-custom">
<span>Save</span><br>
{{ product.compare_at_price_max | minus:product.price | times:100 | divided_by:product.compare_at_price_max | json }}%</span>
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price %}
{% assign percentage = saving | at_least: percentage %}
</div>
{% endif %}

 

If this fixed your issue Likes and Accept as Solution is highly appreciated. Coffee tips fuels my dedication.
Get EXPERIENCED Shopify developers at affordable rates—visit Made4Uo.com for quick quote!
Do not lost your Shopify store! Get FREE trial with ✔️ Rewind Backup: Automatic, reliable, stress-free
Boonphei
Shopify Partner
38 0 6

Hi @made4Uo,

it was unable to display the percentage too. Since the fourth row of this code have the wrong formula so I want to remove it.

 

<!-- <span>{{ product.compare_at_price_max | minus:product.price | times:100 | divided_by:product.compare_at_price_max | json }}%</span> -->

Boonphei_0-1653972602381.png

 

made4Uo
Shopify Partner
3856 717 1199

This is an accepted solution.

Oh. Got it. You just have to add the forloop for variants. Try this

{% if product.compare_at_price >= 0 and product.compare_at_price != null %}
<div class="product-photo-discounted-price-custom">
      {% assign percentage = 0 %}
      {% for variant in product.variants %}
      {% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
      {% assign percentage = saving | at_least: percentage %}
      {% endfor %} 
<span>Save</span><br>
     {{ percentage | append: "%" }}
</div>
{% endif %}

 

If this fixed your issue Likes and Accept as Solution is highly appreciated. Coffee tips fuels my dedication.
Get EXPERIENCED Shopify developers at affordable rates—visit Made4Uo.com for quick quote!
Do not lost your Shopify store! Get FREE trial with ✔️ Rewind Backup: Automatic, reliable, stress-free