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) {
  array(0) {
  array(0) {

How can I get all the active variants for a product?

1 Answer 1


If you already have the product


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


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

If you don't have the product


{% set variants = craft


$variants = Variant::find()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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