From 97995ae728c1a7f93c3a77f6275072b3d3ac2c02 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 13 Aug 2026 08:30:03 -0300 Subject: [PATCH] feat(catalog): replace selectionOptions with selectorOptions to filter hidden attributes --- .../Cart/Resources/CartItemResource.php | 2 +- app/Domains/Catalog/Models/Variant.php | 19 ++++++++++++++ .../Resources/CatalogFeaturedItemResource.php | 2 +- .../Catalog/Resources/CatalogItemResource.php | 2 +- .../Resources/CatalogSearchItemResource.php | 2 +- tests/Unit/Catalog/CatalogModelsTest.php | 25 +++++++++++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/Domains/Cart/Resources/CartItemResource.php b/app/Domains/Cart/Resources/CartItemResource.php index dbe1709..263f716 100644 --- a/app/Domains/Cart/Resources/CartItemResource.php +++ b/app/Domains/Cart/Resources/CartItemResource.php @@ -45,7 +45,7 @@ class CartItemResource extends JsonResource 'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited ? null : $variant->inventory->availableStock(), - 'values' => $variant->selectionOptions($this->catalogItem->itemAttributes), + 'values' => $variant->selectorOptions($this->catalogItem->itemAttributes), ]) ->values(), ], diff --git a/app/Domains/Catalog/Models/Variant.php b/app/Domains/Catalog/Models/Variant.php index 762c987..a66d5bc 100644 --- a/app/Domains/Catalog/Models/Variant.php +++ b/app/Domains/Catalog/Models/Variant.php @@ -268,6 +268,25 @@ class Variant extends Model }); } + /** + * @param Collection $itemAttributes + * @return Collection> + */ + public function selectorOptions(Collection $itemAttributes): Collection + { + $visibleAttributeCodes = $itemAttributes + ->filter(fn (ItemAttribute $itemAttribute): bool => $itemAttribute->show_in_selector) + ->map(fn (ItemAttribute $itemAttribute): ?string => $itemAttribute->attribute?->codigo) + ->filter() + ->values(); + + return $this->selectionOptions($itemAttributes) + ->filter( + fn (array $option, string $attributeCode): bool => $visibleAttributeCodes + ->contains($attributeCode) + ); + } + /** @return Collection */ public function selectedEventDates(): Collection { diff --git a/app/Domains/Catalog/Resources/CatalogFeaturedItemResource.php b/app/Domains/Catalog/Resources/CatalogFeaturedItemResource.php index f528bf2..d9d06d6 100644 --- a/app/Domains/Catalog/Resources/CatalogFeaturedItemResource.php +++ b/app/Domains/Catalog/Resources/CatalogFeaturedItemResource.php @@ -47,7 +47,7 @@ class CatalogFeaturedItemResource extends JsonResource 'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited ? null : $variant->inventory->availableStock(), - 'values' => $variant->selectionOptions($catalogItem->itemAttributes), + 'values' => $variant->selectorOptions($catalogItem->itemAttributes), ]) ->values(), ]; diff --git a/app/Domains/Catalog/Resources/CatalogItemResource.php b/app/Domains/Catalog/Resources/CatalogItemResource.php index d2b0635..fa21c54 100644 --- a/app/Domains/Catalog/Resources/CatalogItemResource.php +++ b/app/Domains/Catalog/Resources/CatalogItemResource.php @@ -46,7 +46,7 @@ class CatalogItemResource extends JsonResource 'descripcion' => $variant->getDescription(), 'precio' => number_format($variant->getPrice(), 2, '.', ''), 'real_stock' => $variant->inventory?->real_stock, - 'values' => $variant->selectionOptions($this->itemAttributes), + 'values' => $variant->selectorOptions($this->itemAttributes), 'images' => $variant->attachments ->map(fn ($attachment) => $attachment->getTemporaryUrl(1440)) ->values(), diff --git a/app/Domains/Catalog/Resources/CatalogSearchItemResource.php b/app/Domains/Catalog/Resources/CatalogSearchItemResource.php index ba12b94..1b6e39c 100644 --- a/app/Domains/Catalog/Resources/CatalogSearchItemResource.php +++ b/app/Domains/Catalog/Resources/CatalogSearchItemResource.php @@ -43,7 +43,7 @@ class CatalogSearchItemResource extends JsonResource 'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited ? null : $variant->inventory?->availableStock(), - 'values' => $variant->selectionOptions($this->itemAttributes), + 'values' => $variant->selectorOptions($this->itemAttributes), ]) ->values(), ]; diff --git a/tests/Unit/Catalog/CatalogModelsTest.php b/tests/Unit/Catalog/CatalogModelsTest.php index d200347..1204a6f 100644 --- a/tests/Unit/Catalog/CatalogModelsTest.php +++ b/tests/Unit/Catalog/CatalogModelsTest.php @@ -271,6 +271,31 @@ class CatalogModelsTest extends TestCase ); } + public function test_variant_excludes_hidden_attributes_from_selector_options(): void + { + $attribute = new Attribute(['codigo' => 'event_date', 'nombre' => 'Fecha']); + $attribute->setRelation('options', new EloquentCollection); + + $itemAttribute = new ItemAttribute([ + 'allow_multi_select' => true, + 'show_in_selector' => false, + ]); + $itemAttribute->id = 1; + $itemAttribute->setRelation('attribute', $attribute); + + $eventDate = new EventDate(['date' => '2026-10-09']); + $eventDate->id = 20; + + $variant = new Variant; + $variant->setRelation('definitions', new EloquentCollection); + $variant->setRelation('eventDates', new EloquentCollection([$eventDate])); + + $this->assertSame( + [], + $variant->selectorOptions(new EloquentCollection([$itemAttribute]))->all(), + ); + } + public function test_inventory_maps_stock_without_a_polymorphic_owner(): void { $inventory = $this->trackedInventory(realStock: 10, reservedStock: 3);