All things Shopify and commerce
I have a question about how you round of your sales badge percentage to the nearest 5 in my collection page. I have one that works on my product page which is this:
{%- if show_badges -%}
<span class="badge price__badge-sale color-{{ settings.sale_badge_color_scheme }}">
{%- assign discount = product.compare_at_price_max | minus: product.price | times: 100.0 | divided_by: product.compare_at_price_max -%}
{%- assign rounded_discount = discount | divided_by: 5 | round | times: 5 -%}
{{ rounded_discount }}% OFF
</span>
{%- endif -%}
Could someone tell me how I can get this on my collection page as well? I also have a code for my collection page sales badge but that doesn't round the numbers, so for example it shows 41% instead of 40%. That code is as following:
{% comment %}
{{ 'products.product.on_sale' | t }}
{% endcomment %}
{{card_product.compare_at_price | minus: card_product.price | times: 100 | divided_by: card_product.compare_at_price }}% OFF
Solved! Go to the solution
This is an accepted solution.
Hey @Tolunay
Try this updated code for the collection page.
{% comment %}
{{ 'products.product.on_sale' | t }}
{% endcomment %}
{% assign discount = card_product.compare_at_price | minus: card_product.price | times: 100 | divided_by: card_product.compare_at_price %}
{% assign rounded_discount = discount | divided_by: 5 | round | times: 5 %}
{{ rounded_discount }}% OFF
If I managed to help you then, don't forget to Like it and Mark it as Solution!
Best Regards,
Moeed
Hello,
Can you share your site url, so that we can give you better suggestions, are you using dawn theme
Thank you
This is an accepted solution.
Hey @Tolunay
Try this updated code for the collection page.
{% comment %}
{{ 'products.product.on_sale' | t }}
{% endcomment %}
{% assign discount = card_product.compare_at_price | minus: card_product.price | times: 100 | divided_by: card_product.compare_at_price %}
{% assign rounded_discount = discount | divided_by: 5 | round | times: 5 %}
{{ rounded_discount }}% OFF
If I managed to help you then, don't forget to Like it and Mark it as Solution!
Best Regards,
Moeed
Wow! That was quick. Thank you very much, that has helped me perfectly!
Thank you for your reply. I'm glad to hear that the solution worked well for you. If you require any more help, please don't hesitate to reach out. If you find this information useful, a Like would be greatly appreciated.
Hi @Moeed,
Sorry for bothering again with this question, but the 2 codes have something different. Some products show for example as 30% on collection page, but 35% on product page. (see pictures) Is there anything that you see in the 2 codes that I can change up to make it the same?
Hey @Tolunay
To make the discount consistent across both product and collection pages, you can ensure you're calculating the discount the same way on both.
Updated Code for Collection Page:
{% comment %}
{{ 'products.product.on_sale' | t }}
{% endcomment %}
{% assign compare_at_price = card_product.compare_at_price_max %}
{% assign price = card_product.price %}
{% assign discount = compare_at_price | minus: price | times: 100 | divided_by: compare_at_price %}
{% assign rounded_discount = discount | divided_by: 5 | round | times: 5 %}
{{ rounded_discount }}% OFF
If that doesn't works then share your collaborator request code in private messages and then I can have a detailed look on it.
If I managed to help you then, don't forget to Like it and Mark it as Solution!
Best Regards,
Moeed
This hasn't worked unfortunately. I'll send you a chat on whatsapp.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024