Ad

Shopify Function For Showing Percentages Of Sale Doesn't Work

- 1 answer

I have a function which should show how many percents the price is off but instead is showing -0 %.

What I have so far is this

{% if settings.use_saleoff and variant_tmp.compare_at_price > variant_tmp.price %}
    <span>{{'products.product.sale' | t}}</span>
    <span class="price_percentage">-{{ variant_tmp.compare_at_price | minus: variant_tmp.price | times: 100.0 | divided_by: variant_tmp.compare_at_price | money_without_currency | times: 100 | remove: '.0'}}%</span>
{% endif %}

Any idea what is wrong here?

Ad

Answer

Here is the right way:

{% if settings.use_saleoff and variant_tmp.compare_at_price > variant_tmp.price %}
    <span>{{'products.product.sale' | t}}</span>
    <span class="price_percentage">{{ 1.00 | times: variant_tmp.price | divided_by: variant_tmp.compare_at_price | times: 100 | round | minus: 100 }}%</span>
{% endif %}

At the first point, you have to show that the number will have decimal point.

Also, you have money_without_currency which will add something like this $100 USD and the letters will break the math logic after that.

Ad
source: stackoverflow.com
Ad