/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $sku = $option->getProductSku(); $product = $this->productRepository->get($sku, true); $optionData = $this->converter->toArray($option); if ($option->getOptionId()) { if (!$product->getOptionById($option->getOptionId())) { throw new NoSuchEntityException(); } $originalValues = $product->getOptionById($option->getOptionId())->getValues(); if (!empty($optionData['values'])) { $optionData['values'] = $this->markRemovedValues($optionData['values'], $originalValues); } } unset($optionData['product_sku']); $product->setProductOptions([$optionData]); $existingOptions = $product->getOptions(); try { $this->productRepository->save($product, true); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save product option')); } $product = $this->productRepository->get($sku, true); if (!$option->getOptionId()) { $currentOptions = $product->getOptions(); if ($existingOptions == null) { $newID = array_keys($currentOptions); } else { $newID = array_diff(array_keys($currentOptions), array_keys($existingOptions)); } if (empty($newID)) { throw new CouldNotSaveException(__('Could not save product option')); } $newID = current($newID); } else { $newID = $option->getOptionId(); } $option = $this->get($sku, $newID); return $option; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $productSku = $option->getProductSku(); if (!$productSku) { throw new CouldNotSaveException(__('ProductSku should be specified')); } $product = $this->productRepository->get($productSku); $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $option->setData('product_id', $product->getData($metadata->getLinkField())); $option->setOptionId(null); $option->save(); return $option; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $product = $this->productRepository->get($option->getProductSku()); $metadata = $this->metadataPool->getMetadata(ProductInterface::class); $option->setData('product_id', $product->getData($metadata->getLinkField())); $option->setOptionId(null); $option->save(); return $option; }