metadataPool = $metadataPool; $this->config = $config; } /** * @param ReadSnapshot $subject * @param array $entityData * @param string $entityType * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterExecute(ReadSnapshot $subject, array $entityData, $entityType) { if (!in_array($entityType, [ProductInterface::class, CategoryInterface::class], true)) { return $entityData; } $metadata = $this->metadataPool->getMetadata($entityType); $connection = $metadata->getEntityConnection(); $globalAttributes = []; $attributesMap = []; $eavEntityType = $metadata->getEavEntityType(); $attributes = null === $eavEntityType ? [] : $this->config->getEntityAttributes($eavEntityType, new \Magento\Framework\DataObject($entityData)); /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ foreach ($attributes as $attribute) { if (!$attribute->isStatic() && $attribute->isScopeGlobal()) { $globalAttributes[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId(); $attributesMap[$attribute->getAttributeId()] = $attribute->getAttributeCode(); } } if ($globalAttributes) { $selects = []; foreach ($globalAttributes as $table => $attributeIds) { $select = $connection->select() ->from( ['t' => $table], ['value' => 't.value', 'attribute_id' => 't.attribute_id'] ) ->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()]) ->where('attribute_id' . ' in (?)', $attributeIds) ->where('store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID); $selects[] = $select; } $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression( $selects, \Magento\Framework\DB\Select::SQL_UNION_ALL ); foreach ($connection->fetchAll($unionSelect) as $attributeValue) { $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value']; } } return $entityData; } }