Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How to sort Product by discount percentage?

How to sort Product by discount percentage?

Anil-pawn
Visitor
1 0 0

{% assign sortedProducts = collection.products | sort: 'discountPercentage' | reverse %}
{% for product in sortedProducts %}
{% assign discountPercentage = product.compare_at_price | minus: product.price | times: 100 | divided_by: product.compare_at_price | round %}
{% if product.available %}
<div class="product"> <h3>{{ product.title }}</h3> <p>{{ product.price }}</p> <p>Discount: {{ product.discountPercentage }}%</p> </div>
{% endif %}
{% endfor %}
</div>
</div>

Replies 2 (2)

NomtechSolution
Astronaut
1245 113 162

To sort products by their discount percentage, you can use the following code:

{% assign sortedProducts = collection.products | sort: 'discountPercentage' | reverse %}
{% for product in sortedProducts %}
  {% assign discountPercentage = product.compare_at_price | minus: product.price | times: 100 | divided_by: product.compare_at_price | round %}
  {% if product.available %}
    <div class="product">
      <h3>{{ product.title }}</h3>
      <p>{{ product.price }}</p>
      <p>Discount: {{ discountPercentage }}%</p>
    </div>
  {% endif %}
{% endfor %}
JohnD310
Shopify Partner
5 0 3

Where should this code be placed?

 

Thanks