Personalized checkout and custom promotions with Shopify Scripts
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.
{% 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 %}
Solved! Go to the solution
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 %}
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 %}
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> -->
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 %}
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024