diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php b/app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php index d490d1ff8cdd4..848b765e77088 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php @@ -1,7 +1,7 @@ _attrOptionCollectionFactory->create() - ->setPositionOrder('asc') - ->setAttributeFilter($this->getAttribute()->getId()) - ->addFieldToFilter('main_table.option_id', ['in' => $ids]) - ->setStoreFilter($this->getAttribute()->getStoreId()) - ->load() - ->toOptionArray(); + $storeId = $this->getAttribute()->getStoreId(); + $attributeId = $this->getAttribute()->getId(); + $optionsIdsToLoad = []; + $options = []; + + $optionIds = is_array($ids) ? $ids : [$ids]; + + foreach ($optionIds as $optionId) { + if (empty($this->_specificOptions[$storeId][$attributeId][$optionId])) { + $optionsIdsToLoad[] = $optionId; + } else { + $options[] = $this->_specificOptions[$storeId][$attributeId][$optionId]; + } + } + + if (!empty($optionsIdsToLoad)) { + $loadedOptions = $this->loadSpecificOptions( + $attributeId, + $optionsIdsToLoad, + $storeId + ); + + array_walk($loadedOptions, function ($loadedOption) use ($storeId, $attributeId, &$options) { + $this->_specificOptions[$storeId][$attributeId][$loadedOption['value']] = $loadedOption; + $options[] = $loadedOption; + }); + } + if ($withEmpty) { $options = $this->addEmptyOption($options); } + return $options; } + /** + * Load specific option in DB + * + * @param mixed $attributeId + * @param array $optionsIdsToLoad + * @param int $storeId + * + * @return array + */ + protected function loadSpecificOptions(mixed $attributeId, array $optionsIdsToLoad, int $storeId): array + { + return $this->_attrOptionCollectionFactory->create() + ->setPositionOrder('asc') + ->setAttributeFilter($attributeId) + ->addFieldToFilter( + 'main_table.option_id', + ['in' => (count($optionsIdsToLoad)>1)?$optionsIdsToLoad:$optionsIdsToLoad[0]] + ) + ->setStoreFilter($storeId) + ->load() + ->toOptionArray(); + } + /** * Add an empty option to the array *