Answers.Body: <p>If you already have the product</p>
  
  <h3>PHP</h3>
  
  <pre class="lang-php prettyprint-override"><code>$product = Product::find()-&gt;one();
  $variants = ArrayHelper::filterByValue($product-&gt;variants, 'enabled', 1);
  </code></pre>
  
  <h3>Twig</h3>
  
  <pre class="lang-twig prettyprint-override"><code>{% set product = craft.products.one() %}
  {% set variants = product.variants|filterByValue('enabled', "1") %}
  </code></pre>
  
  <p>If you don't have the product</p>
  
  <h3>Twig</h3>
  
  <pre class="lang-twig prettyprint-override"><code>{% set variants = craft
      .commerce
      .variants
      .productId(x)
      .all() 
  %}
  </code></pre>
  
  <h3>PHP</h3>
  
  <pre class="lang-php prettyprint-override"><code>$variants = Variant::find()
      -&gt;productId(x)
      -&gt;all();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-20T08:17:38.087Z
Answers.LastActivityDate: 2019-02-20T08:24:17.760Z
Answers.LastEditDate: 2019-02-20T08:24:17.760Z
Answers.UserDisplayName: Robin Schambach
Body: <p>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:</p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/21093/disabled-product-variant-still-shows-in-for-loop">Disabled product variant still shows in {% for %} loop</a></p>
  
  <p>In the comments Andreas provides a way to get all enabled variants for a product:</p>
  
  <p>{% set variants = craft.commerce.variants({ product: product, enabled: true }) %}</p>
  
  <p>However, in my case this does not return any variants, while there are two active. Instead, it returns the following object:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>How can I get all the active variants for a product?</p>
  
Tags: <plugin-craftcommerce><variants>
Title: Get only enabled variants of a product
ThreadText: # Get only enabled variants of a product
  
  (Asked by: username_0 on 2019-02-20)
  
  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](https://craftcms.stackexchange.com/questions/21093/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]=>
    array(0) {
    }
    ["_eventWildcards":"yii\base\Component":private]=>
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=>
    NULL
  }
  
  ```
  
  How can I get all the active variants for a product?
  
  **Tags:** plugin-craftcommerce, variants
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-20. Score: 3 | Accepted answer
  
  If you already have the product
  
  ### PHP
  
  ```php
  $product = Product::find()->one();
  $variants = ArrayHelper::filterByValue($product->variants, 'enabled', 1);
  
  ```
  
  ### Twig
  
  ```twig
  {% set product = craft.products.one() %}
  {% set variants = product.variants|filterByValue('enabled', "1") %}
  
  ```
  
  If you don't have the product
  
  ### Twig
  
  ```twig
  {% set variants = craft
      .commerce
      .variants
      .productId(x)
      .all() 
  %}
  
  ```
  
  ### PHP
  
  ```php
  $variants = Variant::find()
      ->productId(x)
      ->all();
  
  ```
  
  
  ---
  
  