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.