/**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
 {
     if ($saveOptions) {
         $productOptions = $product->getProductOptions();
     }
     $groupPrices = $product->getData('group_price');
     $tierPrices = $product->getData('tier_price');
     $productId = $this->resourceModel->getIdBySku($product->getSku());
     $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
     $product = $this->initializeProductData($productDataArray, empty($productId));
     $validationResult = $this->resourceModel->validate($product);
     if (true !== $validationResult) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
     }
     try {
         if ($saveOptions) {
             $product->setProductOptions($productOptions);
             $product->setCanSaveCustomOptions(true);
         }
         $product->setData('group_price', $groupPrices);
         $product->setData('tier_price', $tierPrices);
         $this->resourceModel->save($product);
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
         throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
     }
     unset($this->instances[$product->getSku()]);
     unset($this->instancesById[$product->getId()]);
     return $product;
 }
Example #2
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
 {
     if ($saveOptions) {
         $productOptions = $product->getProductOptions();
     }
     $isDeleteOptions = $product->getIsDeleteOptions();
     $groupPrices = $product->getData('group_price');
     $tierPrices = $product->getData('tier_price');
     $productId = $this->resourceModel->getIdBySku($product->getSku());
     $ignoreLinksFlag = $product->getData('ignore_links_flag');
     $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
     $productLinks = null;
     if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
         $productLinks = $product->getProductLinks();
     }
     $product = $this->initializeProductData($productDataArray, empty($productId));
     if (isset($productDataArray['options'])) {
         if (!empty($productDataArray['options']) || $isDeleteOptions) {
             $this->processOptions($product, $productDataArray['options']);
             $product->setCanSaveCustomOptions(true);
         }
     }
     $this->processLinks($product, $productLinks);
     if (isset($productDataArray['media_gallery_entries'])) {
         $this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
     }
     $validationResult = $this->resourceModel->validate($product);
     if (true !== $validationResult) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
     }
     try {
         if ($saveOptions) {
             $product->setProductOptions($productOptions);
             $product->setCanSaveCustomOptions(true);
         }
         if ($groupPrices !== null) {
             $product->setData('group_price', $groupPrices);
         }
         if ($tierPrices !== null) {
             $product->setData('tier_price', $tierPrices);
         }
         $this->resourceModel->save($product);
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
         throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
     }
     unset($this->instances[$product->getSku()]);
     unset($this->instancesById[$product->getId()]);
     return $this->get($product->getSku());
 }