1

I'm currently able to return the first variant using the following:

<?php
use craft\commerce\elements\Product;
use craft\helpers\UrlHelper;

return [
  'endpoints' => [
    '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: "<p>This is another 3M variant</p>",
      purchasableId: 37,
      status: "enabled",
      url: "https://testing.kamicrafted.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.

1 Answer 1

1

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;
        }
1
  • 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 :) Commented Nov 1, 2019 at 22:21

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.