/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save($sku, OptionInterface $option) { /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */ $configurableAttribute = $this->configurableAttributeFactory->create(); if ($option->getId()) { /** @var Product $product */ $product = $this->getProduct($sku); $configurableAttribute->load($option->getId()); if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) { throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId())); } $configurableAttribute->addData($option->getData()); $configurableAttribute->setValues($option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()); try { $configurableAttribute->save(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not update option with id "%1"', $option->getId())); } } else { $this->validateNewOptionData($option); /** @var Product $product */ $product = $this->productRepository->get($sku); $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE]; if (!in_array($product->getTypeId(), $allowedTypes)) { throw new \InvalidArgumentException('Incompatible product type'); } $eavAttribute = $this->productAttributeRepository->get($option->getAttributeId()); $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute); if ($configurableAttribute->getId()) { throw new CouldNotSaveException(__('Product already has this option')); } $configurableAttributesData = ['attribute_id' => $option->getAttributeId(), 'position' => $option->getPosition(), 'use_default' => $option->getIsUseDefault(), 'label' => $option->getLabel(), 'values' => $option->getValues()]; try { $product->setTypeId(ConfigurableType::TYPE_CODE); $product->setConfigurableAttributesData([$configurableAttributesData]); $product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId()); $product->save(); } catch (\Exception $e) { throw new CouldNotSaveException(__('An error occurred while saving option')); } $configurableAttribute = $this->configurableAttributeFactory->create(); $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute); } if (!$configurableAttribute->getId()) { throw new CouldNotSaveException(__('An error occurred while saving option')); } return $configurableAttribute->getId(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save($sku, OptionInterface $option) { $metadata = $this->metadataPool->getMetadata(ProductInterface::class); if ($option->getId()) { /** @var Product $product */ $product = $this->getProduct($sku); $data = $option->getData(); $option->load($option->getId()); $option->setData(array_replace_recursive($option->getData(), $data)); if (!$option->getId() || $option->getProductId() != $product->getData($metadata->getLinkField())) { throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId())); } } else { /** @var Product $product */ $product = $this->productRepository->get($sku); $this->validateNewOptionData($option); $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE]; if (!in_array($product->getTypeId(), $allowedTypes)) { throw new \InvalidArgumentException('Incompatible product type'); } $option->setProductId($product->getData($metadata->getLinkField())); } try { $option->save(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Something went wrong while saving option.')); } if (!$option->getId()) { throw new CouldNotSaveException(__('Something went wrong while saving option.')); } return $option->getId(); }