/** * Remove product attributes * * @param ProductInterface $product * @return void */ private function deleteConfigurableProductAttributes(ProductInterface $product) { $list = $this->optionRepository->getList($product->getSku()); foreach ($list as $item) { $this->optionRepository->deleteById($product->getSku(), $item->getId()); } }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $options * @return $this */ protected function saveConfigurableProductOptions(\Magento\Catalog\Api\Data\ProductInterface $product, array $options) { $existingOptionIds = []; if ($product->getExtensionAttributes() !== null) { $extensionAttributes = $product->getExtensionAttributes(); if ($extensionAttributes->getConfigurableProductOptions() !== null) { $existingOptions = $extensionAttributes->getConfigurableProductOptions(); foreach ($existingOptions as $option) { $existingOptionIds[] = $option->getId(); } } } $updatedOptionIds = []; foreach ($options as $option) { if ($option->getId()) { $updatedOptionIds[] = $option->getId(); } $this->optionRepository->save($product->getSku(), $option); } $optionIdsToDelete = array_diff($existingOptionIds, $updatedOptionIds); foreach ($optionIdsToDelete as $optionId) { $this->optionRepository->deleteById($product->getSku(), $optionId); } return $this; }