<p>This should filter the variants by their enabled status:</p>

<pre><code>{% set product = craft.commerce.products.id(productId).first() %}

{% for variant in product.variants if variant.enabled %}
    {{ variant.id }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-07-07T00:31:13.633Z
It's always so simple, I feel so dumb for not even trying that! :)
Anders
CC BY-SA 3.0
2017-07-07T00:36:26.493Z
On the other hand, you would think a disabled variant would be hidden by default unless included through templating.
Anders
CC BY-SA 3.0
2018-01-09T15:50:08.633Z
I agree; you would think that `product.variants` would only return enabled products. How would you test to see if a product has enabled variants? Would you need to loop through and create a new array with only enabled variants? Or is there some property to access only enabled variants.
Trevor Davis
CC BY-SA 4.0
2018-08-10T09:22:47.597Z
To get only enabled variants of a product you can do it like this: 

     `{% set variants = craft.commerce.variants({
                                    product: product,
                                    enabled: true
                                }) %}`
Andreas
CC BY-SA 3.0
2017-07-06T19:20:46.633Z
2017-07-06T19:20:46.633Z
Brad Bell
<p>Disabling a product variant doesn't remove it from a <code>{% for purchasable in product.variants %}</code> loop I have set up. It still renders an image associated with it, as well as a color swatch. Essentially not disabling it at all.</p>

<p><code>{% for purchasable in product.variants if purchasable.status('live') %}</code> isn't working either, which I assume is only applicable to the product model.</p>

<p>Any ideas?</p>

<templating><plugin-craftcommerce>
Disabled product variant still shows in {% for %} loop
# Disabled product variant still shows in {% for %} loop

(Asked by: username_0 on 2017-07-06)

Disabling a product variant doesn't remove it from a `{% for purchasable in product.variants %}` loop I have set up. It still renders an image associated with it, as well as a color swatch. Essentially not disabling it at all.

`{% for purchasable in product.variants if purchasable.status('live') %}` isn't working either, which I assume is only applicable to the product model.

Any ideas?

**Tags:** templating, plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2017-07-06. Score: 2 | Accepted answer

This should filter the variants by their enabled status:
{% set product = craft.commerce.products.id(productId).first() %}

{% for variant in product.variants if variant.enabled %}
    {{ variant.id }}
{% endfor %}

> **Comments:**
> **username_0**: It's always so simple, I feel so dumb for not even trying that! :)  
> **username_0**: On the other hand, you would think a disabled variant would be hidden by default unless included through templating.  
> **username_2**: I agree; you would think that `product.variants` would only return enabled products. How would you test to see if a product has enabled variants? Would you need to loop through and create a new array with only enabled variants? Or is there some property to access only enabled variants.  
> **username_3**: To get only enabled variants of a product you can do it like this:        `{% set variants = craft.commerce.variants({                                     product: product,                                     enabled: true                                 }) %}`  

---

