/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param int[] $linkIds * @return $this */ protected function saveConfigurableProductLinks(\Magento\Catalog\Api\Data\ProductInterface $product, array $linkIds) { $configurableProductTypeResource = $this->typeConfigurableFactory->create(); if (!empty($linkIds)) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableProductType */ $configurableProductType = $product->getTypeInstance(); $configurableAttributes = $configurableProductType->getConfigurableAttributes($product); $attributeCodes = []; foreach ($configurableAttributes as $configurableAttribute) { /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $productAttribute */ $productAttribute = $configurableAttribute->getProductAttribute(); $attributeCode = $productAttribute->getAttributeCode(); $attributeCodes[] = $attributeCode; } $this->validateProductLinks($attributeCodes, $linkIds); } $configurableProductTypeResource->saveProducts($product, $linkIds); return $this; }
/** * Delete data specific for Configurable product type * * @param \Magento\Catalog\Model\Product $product */ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) { $this->typeConfigurableFactory->create()->saveProducts($product, []); /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */ $configurableAttribute = $this->configurableAttributeFactory->create(); $configurableAttribute->deleteByProduct($product); }