diff --git a/Model/Write/EavIterator.php b/Model/Write/EavIterator.php index 4394984..8fc1fb4 100644 --- a/Model/Write/EavIterator.php +++ b/Model/Write/EavIterator.php @@ -113,6 +113,11 @@ class EavIterator implements IteratorAggregate */ private array $parentRelations = []; + /** + * @var array + */ + private array $parentRelationsImageAttributes = []; + /** * EavIterator constructor. * @@ -276,6 +281,7 @@ public function getIterator(): \Traversable $this->setEntityIds($entityIds); if ($this->config->isGroupedExport($this->store) && $this->entityCode === Product::ENTITY) { $this->preloadParentRelations($entityIds); + $this->preloadConfigurableImageData(); } $select = $this->createSelect(); @@ -303,6 +309,7 @@ public function getIterator(): \Traversable isset($this->parentRelations[$result['entity_id']]) ) { $result['parent_id'] = $this->parentRelations[$result['entity_id']]; + $result = $this->setImagesAttributesFromConfigurable($result); } yield $result; @@ -576,4 +583,50 @@ protected function preloadParentRelations(array $productIds): void $this->parentRelations = $connection->fetchPairs($select); } + + /** + * @return void + */ + protected function preloadConfigurableImageData(): void + { + $uniqueConfigurableIds = array_unique($this->parentRelations); + if (empty($uniqueConfigurableIds)) { + return; + } + + $imageAttributes = ['image', 'small_image', 'thumbnail']; + $connection = $this->getConnection(); + $select = $connection->select() + ->from( + ['cpev' => 'catalog_product_entity_varchar'], + ['entity_id' => 'cpev.entity_id', 'value' => 'cpev.value'] + ) + ->join( + ['ea' => 'eav_attribute'], + 'ea.attribute_id = cpev.attribute_id', + ['attribute_code' => 'ea.attribute_code'] + ) + ->where('ea.attribute_code IN (?)', $imageAttributes) + ->where('cpev.entity_id IN (?)', $uniqueConfigurableIds); + + $result = $connection->fetchAll($select); + foreach ($result as $row) { + $this->parentRelationsImageAttributes[$row['entity_id']][$row['attribute_code']] = $row['value']; + } + } + + /** + * @param array $attributes + * + * @return array + */ + protected function setImagesAttributesFromConfigurable(array $attributes): array + { + $parentId = $attributes['parent_id']; + foreach ($this->parentRelationsImageAttributes[$parentId] ?? [] as $attribute => $attributeValue) { + $attributes[$attribute] = $attributeValue; + } + + return $attributes; + } }