feat(catalog): replace selectionOptions with selectorOptions to filter hidden attributes
This commit is contained in:
@@ -45,7 +45,7 @@ class CartItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory->availableStock(),
|
: $variant->inventory->availableStock(),
|
||||||
'values' => $variant->selectionOptions($this->catalogItem->itemAttributes),
|
'values' => $variant->selectorOptions($this->catalogItem->itemAttributes),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -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> */
|
/** @return Collection<int, EventDate> */
|
||||||
public function selectedEventDates(): Collection
|
public function selectedEventDates(): Collection
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ class CatalogFeaturedItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory->availableStock(),
|
: $variant->inventory->availableStock(),
|
||||||
'values' => $variant->selectionOptions($catalogItem->itemAttributes),
|
'values' => $variant->selectorOptions($catalogItem->itemAttributes),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class CatalogItemResource extends JsonResource
|
|||||||
'descripcion' => $variant->getDescription(),
|
'descripcion' => $variant->getDescription(),
|
||||||
'precio' => number_format($variant->getPrice(), 2, '.', ''),
|
'precio' => number_format($variant->getPrice(), 2, '.', ''),
|
||||||
'real_stock' => $variant->inventory?->real_stock,
|
'real_stock' => $variant->inventory?->real_stock,
|
||||||
'values' => $variant->selectionOptions($this->itemAttributes),
|
'values' => $variant->selectorOptions($this->itemAttributes),
|
||||||
'images' => $variant->attachments
|
'images' => $variant->attachments
|
||||||
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
||||||
->values(),
|
->values(),
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class CatalogSearchItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory?->availableStock(),
|
: $variant->inventory?->availableStock(),
|
||||||
'values' => $variant->selectionOptions($this->itemAttributes),
|
'values' => $variant->selectorOptions($this->itemAttributes),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -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
|
public function test_inventory_maps_stock_without_a_polymorphic_owner(): void
|
||||||
{
|
{
|
||||||
$inventory = $this->trackedInventory(realStock: 10, reservedStock: 3);
|
$inventory = $this->trackedInventory(realStock: 10, reservedStock: 3);
|
||||||
|
|||||||
Reference in New Issue
Block a user