Get only enabled variants of a product I would like to get all enabled variants for a product. I refer to the following question, where a method for limiting a for-loop is given:

Disabled product variant still shows in {% for %} loop

In the comments Andreas provides a way to get all enabled variants for a product:

{% set variants = craft.commerce.variants({ product: product, enabled: true }) %}

However, in my case this does not return any variants, while there are two active. Instead, it returns the following object:

object(craft\commerce\services\Variants)#2364 (3) {
  ["_events":"yii\base\Component":private]=&gt;
  array(0) {
  }
  ["_eventWildcards":"yii\base\Component":private]=&gt;
  array(0) {
  }
  ["_behaviors":"yii\base\Component":private]=&gt;
  NULL
}


How can I get all the active variants for a product?
If you already have the product

PHP

$product = Product::find()-&gt;one();
$variants = ArrayHelper::filterByValue($product-&gt;variants, 'enabled', 1);


Twig

{% set product = craft.products.one() %}
{% set variants = product.variants|filterByValue('enabled', "1") %}


If you don't have the product

Twig

{% set variants = craft
    .commerce
    .variants
    .productId(x)
    .all() 
%}


PHP

$variants = Variant::find()
    -&gt;productId(x)
    -&gt;all();