feat(catalog): replace selectionOptions with selectorOptions to filter hidden attributes

This commit is contained in:
2026-08-13 08:30:03 -03:00
parent 7c7a295625
commit 97995ae728
6 changed files with 48 additions and 4 deletions

View File

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

View File

@@ -268,6 +268,25 @@ class Variant extends Model
});
}
/**
* @param Collection<int, ItemAttribute> $itemAttributes
* @return Collection<string, array{value: string, label: string}|array<int, array{value: string, label: string}>>
*/
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<int, EventDate> */
public function selectedEventDates(): Collection
{

View File

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

View File

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

View File

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

View File

@@ -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);