attributeCollectionFactory = $attributeCollectionFactory; $this->productFactory = $productFactory; $this->metadataPool = $metadataPool; } /** * Add product id to attribute collection filter. * * @param int $productId */ public function addProductId(int $productId) : void { if (!in_array($productId, $this->productIds)) { $this->productIds[] = $productId; } } /** * Retrieve attributes for given product id or empty array * * @param int $productId * @return array */ public function getAttributesByProductId(int $productId) : array { $attributes = $this->fetch(); if (!isset($attributes[$productId])) { return []; } return $attributes[$productId]; } /** * Fetch attribute data * * @return array */ private function fetch() : array { if (empty($this->productIds) || !empty($this->attributeMap)) { return $this->attributeMap; } $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); /** @var AttributeCollection $attributeCollection */ $attributeCollection = $this->attributeCollectionFactory->create(); foreach ($this->productIds as $id) { /** @var Product $product */ $product = $this->productFactory->create(); $product->setData($linkField, $id); $attributeCollection->setProductFilter($product); } /** @var Attribute $attribute */ foreach ($attributeCollection->getItems() as $attribute) { $productId = (int)$attribute->getProductId(); if (!isset($this->attributeMap[$productId])) { $this->attributeMap[$productId] = []; } $attributeData = $attribute->getData(); $this->attributeMap[$productId][$attribute->getId()] = $attribute->getData(); $this->attributeMap[$productId][$attribute->getId()]['id'] = $attribute->getId(); $this->attributeMap[$productId][$attribute->getId()]['attribute_code'] = $attribute->getProductAttribute()->getAttributeCode(); $this->attributeMap[$productId][$attribute->getId()]['values'] = $attributeData['options']; $this->attributeMap[$productId][$attribute->getId()]['label'] = $attribute->getProductAttribute()->getStoreLabel(); } return $this->attributeMap; } }