2

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?

2

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 %}
| improve this answer | |
  • It's always so simple, I feel so dumb for not even trying that! :) – Anders Jul 7 '17 at 0:31
  • 2
    On the other hand, you would think a disabled variant would be hidden by default unless included through templating. – Anders Jul 7 '17 at 0:36
  • 1
    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 Jan 9 '18 at 15:50
  • To get only enabled variants of a product you can do it like this: {% set variants = craft.commerce.variants({ product: product, enabled: true }) %} – Andreas Aug 10 '18 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.