<p>the following query possibly produce the similar result</p>
<pre><code>{% set variantsHasSales = craft.variants()
    .product(product)
    .hasSales(true)
    .all() 
%}
{% if variantsHasSales|length &gt; 0 %}
  code here for sales badge/message etc
{% endif %}
</code></pre>
<p>reference : <a href="https://craftcms.com/docs/commerce/3.x/products-variants.html#variant-hassales" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/products-variants.html#variant-hassales</a></p>

CC BY-SA 4.0
2021-08-19T16:50:12.073Z
2021-08-19T16:50:12.073Z
Dzulfikar Adi Putra
<p>So the only way I could seem to make this work was as follows:</p>
<pre><code>{% set onSale = product.variants|filter(v =&gt; v.onSale())|length %}

{% if onSale %}
   code here for sales badge/message etc
{% endif %}
</code></pre>
<p>Not sure if there is a more direct method? If there is please let me know...</p>

CC BY-SA 4.0
2021-08-13T17:15:49.283Z
2021-08-13T17:15:49.283Z
Terry Upton
<p>I want to check if any variants (not the default variant) have any active sales applied.
How can I do this at a product level when looping through products (think teasers).</p>

<plugin-craftcommerce>
How can I check if a product has any variant that is on Sale?
# How can I check if a product has any variant that is on Sale?

(Asked by: username_0 on 2021-08-13)

I want to check if any variants (not the default variant) have any active sales applied.
How can I do this at a product level when looping through products (think teasers).

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_1 on 2021-08-19. Score: 1

the following query possibly produce the similar result
{% set variantsHasSales = craft.variants()
    .product(product)
    .hasSales(true)
    .all() 
%}
{% if variantsHasSales|length > 0 %}
  code here for sales badge/message etc
{% endif %}

reference : https://craftcms.com/docs/commerce/3.x/products-variants.html#variant-hassales


---

### Answer by username_0 on 2021-08-13. Score: 1

So the only way I could seem to make this work was as follows:
{% set onSale = product.variants|filter(v => v.onSale())|length %}

{% if onSale %}
   code here for sales badge/message etc
{% endif %}

Not sure if there is a more direct method? If there is please let me know...


---

