<p>You are setting the whole <code>$variants</code> variable to last item in the array instead of creating the <code>$variants</code> array variable and pushing items into it. Should be this:</p>

<pre><code>'transformer' =&gt; function (Product $product) {
          $variants = [];

          foreach ( $product-&gt;getVariants() as $variant ) {
            $variants[] = [
              'type'          =&gt; $product-&gt;type-&gt;name,
              'title'         =&gt; $variant-&gt;title,
              'sku'           =&gt; $variant-&gt;sku,
              'price'         =&gt; $variant-&gt;price,
              'stock'         =&gt; $variant-&gt;stock,
              'intro'         =&gt; $variant-&gt;intro,
              'purchasableId' =&gt; (int) $variant-&gt;purchasableId,
              'status'        =&gt; $variant-&gt;status,
              'url'           =&gt; $variant-&gt;url,
              // 'productPhoto'  =&gt; $variant-&gt;productPhoto
            ];
          }

          return $variants;
        }
</code></pre>

CC BY-SA 4.0
2019-11-01T22:21:58.140Z
thank you so much! i'm stumbling my way through element API so your help is much appreciated. i can see what i was doing wrong now :)
kamicrafted
CC BY-SA 4.0
2019-10-30T03:54:07.373Z
2019-10-30T03:54:07.373Z
Luke Holder
<p>I'm currently able to return the first variant using the following:</p>

<pre><code>&lt;?php
use craft\commerce\elements\Product;
use craft\helpers\UrlHelper;

return [
  'endpoints' =&gt; [
    'products.json' =&gt; function () {
      return [
        'elementType' =&gt; Product::class,
        'paginate' =&gt; false,
        'pretty' =&gt; true,
        'criteria' =&gt; [
          // 'id' =&gt; $productId,
        ],
        'transformer' =&gt; function (Product $product) {
          foreach ( $product-&gt;getVariants() as $variant ) {
            $variants = [
              'type'          =&gt; $product-&gt;type-&gt;name,
              'title'         =&gt; $variant-&gt;title,
              'sku'           =&gt; $variant-&gt;sku,
              'price'         =&gt; $variant-&gt;price,
              'stock'         =&gt; $variant-&gt;stock,
              'intro'         =&gt; $variant-&gt;intro,
              'purchasableId' =&gt; (int) $variant-&gt;purchasableId,
              'status'        =&gt; $variant-&gt;status,
              'url'           =&gt; $variant-&gt;url,
              // 'productPhoto'  =&gt; $variant-&gt;productPhoto
            ];
          }

          return $variants;
        }
      ];
    }
  ]
]
?&gt;
</code></pre>

<p>Which returns:</p>

<pre><code>{
  data: [
    {
      type: "Shakes",
      title: "3 Month Supply",
      sku: "mvn-a03m",
      price: "100.0000",
      stock: "0",
      intro: "&lt;p&gt;This is another 3M variant&lt;/p&gt;",
      purchasableId: 37,
      status: "enabled",
      url: "https://testing.kamicrafted.com/shop/products/mavenshake?variant=37"
    }
  ]
}
</code></pre>

<p>The issue is that I currently have 3 variants set for this product yet the json response only shows the last variant within the set.</p>

<p>I'm completely stumped at this point and was hoping someone had a similar experience with a solution to this issue.</p>

<plugin-craftcommerce><plugin-elementapi><plugin-craftcommerce2>
Having issues looping through variants for Commerce 2 Products using Element API
# Having issues looping through variants for Commerce 2 Products using Element API

(Asked by: username_0 on 2019-10-30)

I'm currently able to return the first variant using the following:
 [
    'products.json' => function () {
      return [
        'elementType' => Product::class,
        'paginate' => false,
        'pretty' => true,
        'criteria' => [
          // 'id' => $productId,
        ],
        'transformer' => function (Product $product) {
          foreach ( $product->getVariants() as $variant ) {
            $variants = [
              'type'          => $product->type->name,
              'title'         => $variant->title,
              'sku'           => $variant->sku,
              'price'         => $variant->price,
              'stock'         => $variant->stock,
              'intro'         => $variant->intro,
              'purchasableId' => (int) $variant->purchasableId,
              'status'        => $variant->status,
              'url'           => $variant->url,
              // 'productPhoto'  => $variant->productPhoto
            ];
          }

          return $variants;
        }
      ];
    }
  ]
]
?>

Which returns:
{
  data: [
    {
      type: "Shakes",
      title: "3 Month Supply",
      sku: "mvn-a03m",
      price: "100.0000",
      stock: "0",
      intro: "This is another 3M variant",
      purchasableId: 37,
      status: "enabled",
      url: "https://testing.username_0.com/shop/products/mavenshake?variant=37"
    }
  ]
}

The issue is that I currently have 3 variants set for this product yet the json response only shows the last variant within the set.
I'm completely stumped at this point and was hoping someone had a similar experience with a solution to this issue.

**Tags:** plugin-craftcommerce, plugin-elementapi, plugin-craftcommerce2


---

## 1 answers

### Answer by username_1 on 2019-10-30. Score: 1 | Accepted answer

You are setting the whole $variants variable to last item in the array instead of creating the $variants array variable and pushing items into it. Should be this:
'transformer' => function (Product $product) {
          $variants = [];

          foreach ( $product->getVariants() as $variant ) {
            $variants[] = [
              'type'          => $product->type->name,
              'title'         => $variant->title,
              'sku'           => $variant->sku,
              'price'         => $variant->price,
              'stock'         => $variant->stock,
              'intro'         => $variant->intro,
              'purchasableId' => (int) $variant->purchasableId,
              'status'        => $variant->status,
              'url'           => $variant->url,
              // 'productPhoto'  => $variant->productPhoto
            ];
          }

          return $variants;
        }

> **Comments:**
> **username_0**: thank you so much! i'm stumbling my way through element API so your help is much appreciated. i can see what i was doing wrong now :)  

---

