How can I round off sales badge percentage to the nearest 5 in collection page?

Solved

How can I round off sales badge percentage to the nearest 5 in collection page?

Tolunay
Excursionist
24 0 4

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

Accepted Solution (1)

Moeed
Shopify Partner
5346 1445 1730

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

- Need a Shopify Specialist? Chat on WhatsApp

- Custom Design | Advanced Coding | Store Modifications


View solution in original post

Replies 7 (7)

Arif_Shopidevs
Shopify Partner
263 36 36

Hello,
Can you share your site url, so that we can give you better suggestions, are you using dawn theme

Thank you

Found it helpful? Please like and mark the solution that helped you.
Slider Revolution - Create sliders, theme sections, banners, videos, pages, advanced animation, and social feeds.
Essential Grid Gallery - Create photo galleries, video galleries, portfolio galleries, product gallery, collection gallery and more.
EasyDisplay: Product Showcase - Easily display collections, related products, discounts, recently viewed items, and best sellers

Moeed
Shopify Partner
5346 1445 1730

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

- Need a Shopify Specialist? Chat on WhatsApp

- Custom Design | Advanced Coding | Store Modifications


Tolunay
Excursionist
24 0 4

Wow! That was quick. Thank you very much, that has helped me perfectly!

Moeed
Shopify Partner
5346 1445 1730

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.

- Need a Shopify Specialist? Chat on WhatsApp

- Custom Design | Advanced Coding | Store Modifications


Tolunay
Excursionist
24 0 4

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?
% foto 2.jpg% foto.jpg

Moeed
Shopify Partner
5346 1445 1730

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

- Need a Shopify Specialist? Chat on WhatsApp

- Custom Design | Advanced Coding | Store Modifications


Tolunay
Excursionist
24 0 4

This hasn't worked unfortunately. I'll send you a chat on whatsapp.