feat: enhance product detail retrieval to support variant selection and update resource fields
This commit is contained in:
@@ -32,16 +32,17 @@ class ProductResource extends JsonResource
|
||||
),
|
||||
'attributes' => AttributeResource::collection($this->whenLoaded('attributes')),
|
||||
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
|
||||
'default_variant' => $this->when(
|
||||
$this->getDefaultVariant() !== null,
|
||||
'variant' => $this->when(
|
||||
$this->getSelectedVariant() !== null,
|
||||
function () {
|
||||
$defaultVariant = $this->getDefaultVariant();
|
||||
$selectedVariant = $this->getSelectedVariant();
|
||||
|
||||
return [
|
||||
'variant_id' => $defaultVariant->id,
|
||||
'images' => $defaultVariant->attachments->isNotEmpty()
|
||||
? $defaultVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
'variant_id' => $selectedVariant->id,
|
||||
'images' => $selectedVariant->attachments->isNotEmpty()
|
||||
? $selectedVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
|
||||
'attributes' => $defaultVariant->definitions->mapWithKeys(function ($definition) {
|
||||
'attributes' => $selectedVariant->definitions->mapWithKeys(function ($definition) {
|
||||
return [$definition->attribute?->codigo => $definition->value];
|
||||
})->toArray(),
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user