feat: enhance product detail retrieval to support variant selection and update resource fields

This commit is contained in:
2026-06-30 16:21:15 -03:00
parent 1d0f9e323d
commit 191a40fcb9
4 changed files with 36 additions and 20 deletions

View File

@@ -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(),
];