productFactory = $productFactory; $this->customAttributeFactory = $customAttributeFactory; $this->variationMatrix = $variationMatrix; } /** * Populate product with variation of attributes * * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param array $attributes * @return \Magento\Catalog\Api\Data\ProductInterface[] */ public function create(\Magento\Catalog\Api\Data\ProductInterface $product, $attributes) { $variations = $this->variationMatrix->getVariations($attributes); $products = []; foreach ($variations as $variation) { $price = $product->getPrice(); /** @var \Magento\Catalog\Model\Product $item */ $item = $this->productFactory->create(); $item->setData($product->getData()); $suffix = ''; foreach ($variation as $attributeId => $valueInfo) { $suffix .= '-' . $valueInfo['value']; $customAttribute = $this->customAttributeFactory->create() ->setAttributeCode($attributes[$attributeId]['attribute_code']) ->setValue($valueInfo['value']); $customAttributes = array_merge( $item->getCustomAttributes(), [ $attributes[$attributeId]['attribute_code'] => $customAttribute ] ); $item->setData('custom_attributes', $customAttributes); } $item->setPrice($price); $item->setName($product->getName() . $suffix); $item->setSku($product->getSku() . $suffix); $item->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE); $products[] = $item; } return $products; } }