feat: refactor product attributes handling to use product_attribute table and update related models and requests

This commit is contained in:
2026-06-30 16:35:56 -03:00
parent 191a40fcb9
commit cb15905c46
15 changed files with 189 additions and 63 deletions

View File

@@ -43,7 +43,7 @@ class ProductResource extends JsonResource
? $selectedVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
'attributes' => $selectedVariant->definitions->mapWithKeys(function ($definition) {
return [$definition->attribute?->codigo => $definition->value];
return [$definition->productAttribute?->attribute?->codigo => $definition->value];
})->toArray(),
];
}

View File

@@ -18,9 +18,10 @@ class ProductVariantDefinitionResource extends JsonResource
return [
'id' => $this->id,
'producto_variante_id' => $this->producto_variante_id,
'attribute_id' => $this->attribute_id,
'products_attribute_id' => $this->products_attribute_id,
'attribute_id' => $this->whenLoaded('productAttribute', fn () => $this->productAttribute?->attribute_id),
'value' => $this->value,
'attribute' => $this->whenLoaded('attribute', fn () => $this->attribute?->nombre),
'attribute' => $this->whenLoaded('productAttribute', fn () => $this->productAttribute?->attribute?->nombre),
'metadata' => $this->resolveMetadata(),
];
}
@@ -30,11 +31,17 @@ class ProductVariantDefinitionResource extends JsonResource
*/
protected function resolveMetadata(): ?array
{
if (! $this->relationLoaded('attribute') || ! $this->attribute?->relationLoaded('options')) {
if (! $this->relationLoaded('productAttribute')) {
return null;
}
$option = $this->attribute->options->firstWhere('value', $this->value);
$attribute = $this->productAttribute?->attribute;
if (! $attribute?->relationLoaded('options')) {
return null;
}
$option = $attribute->options->firstWhere('value', $this->value);
if ($option === null || $option->metadata === null) {
return null;