Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app/code/Magento/Swatches/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class Data
*/
private $swatchesCache = [];

/**
* @var array
*/
private $attributeOptionsCache = [];

/**
* @var Json
*/
Expand Down Expand Up @@ -438,7 +443,7 @@ public function getSwatchAttributesAsArray(ProductInterface $product)
foreach ($swatchAttributes as $swatchAttribute) {
$swatchAttribute->setStoreId($this->storeManager->getStore()->getId());
$attributeData = $swatchAttribute->getData();
foreach ($swatchAttribute->getSource()->getAllOptions(false) as $option) {
foreach ($this->getAttributeOptions($swatchAttribute, (int)$attributeData['attribute_id']) as $option) {
$attributeData['options'][$option['value']] = $option['label'];
}
$result[$attributeData['attribute_id']] = $attributeData;
Expand All @@ -447,6 +452,24 @@ public function getSwatchAttributesAsArray(ProductInterface $product)
return $result;
}

/**
* Retrieve attribute options with per-instance memoization.
*
* @param Attribute $attribute
* @param int $attributeId
* @return array
*/
private function getAttributeOptions(Attribute $attribute, int $attributeId): array
{
$cacheKey = $attributeId . '_' . (int)$attribute->getStoreId();

if (!isset($this->attributeOptionsCache[$cacheKey])) {
$this->attributeOptionsCache[$cacheKey] = $attribute->getSource()->getAllOptions(false);
}

return $this->attributeOptionsCache[$cacheKey];
}

/**
* Get swatch options by option id's according to fallback logic
*
Expand Down
56 changes: 56 additions & 0 deletions app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,62 @@ public function testGetSwatchAttributesAsArray($optionsArray, $attributeData, $e
$this->assertEquals($result, $expected);
}

/**
* @return void
*/
public function testGetSwatchAttributesAsArrayMemoizesAttributeOptionsPerStore(): void
{
$storeOneOptions = [
['value' => 45, 'label' => 'green'],
['value' => 46, 'label' => 'yellow']
];
$storeTwoOptions = [
['value' => 45, 'label' => 'grun'],
['value' => 46, 'label' => 'gelb']
];
$attributeData = ['attribute_id' => 52];
$expectedStoreOne = [
52 => [
'attribute_id' => 52,
'options' => [
45 => 'green',
46 => 'yellow'
]
]
];
$expectedStoreTwo = [
52 => [
'attribute_id' => 52,
'options' => [
45 => 'grun',
46 => 'gelb'
]
]
];

$this->swatchAttributesProvider
->method('provide')
->with($this->productMock)
->willReturn([$this->attributeMock]);

$storeMock = $this->createMock(Store::class);
$storeMock->method('getId')->willReturnOnConsecutiveCalls(1, 1, 2);
$this->storeManagerMock->method('getStore')->willReturn($storeMock);

$this->attributeMock->method('getData')->with('')->willReturn($attributeData);

$sourceMock = $this->createMock(AbstractSource::class);
$sourceMock->expects($this->exactly(2))
->method('getAllOptions')
->with(false)
->willReturnOnConsecutiveCalls($storeOneOptions, $storeTwoOptions);
$this->attributeMock->method('getSource')->willReturn($sourceMock);

$this->assertEquals($expectedStoreOne, $this->swatchHelperObject->getSwatchAttributesAsArray($this->productMock));
$this->assertEquals($expectedStoreOne, $this->swatchHelperObject->getSwatchAttributesAsArray($this->productMock));
$this->assertEquals($expectedStoreTwo, $this->swatchHelperObject->getSwatchAttributesAsArray($this->productMock));
}

/**
* @return array
*/
Expand Down