Example #1
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();
        $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 ($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());
    }
 public function testValidateWrongAttributeSet()
 {
     $productTypeId = 4;
     $expectedErrorMessage = ['attribute_set' => 'Invalid attribute set entity type'];
     $productMock = $this->getMock('\\Magento\\Framework\\DataObject', ['getAttributeSetId', '__wakeup'], [], '', false);
     $attributeSetMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute\\Set', ['load', 'getEntityTypeId', '__wakeup'], [], '', false);
     $entityTypeMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Type', [], [], '', false);
     $this->typeFactoryMock->expects($this->once())->method('create')->will($this->returnValue($entityTypeMock));
     $entityTypeMock->expects($this->once())->method('loadByCode')->with('catalog_product')->willReturnSelf();
     $productAttributeSetId = 4;
     $productMock->expects($this->once())->method('getAttributeSetId')->will($this->returnValue($productAttributeSetId));
     $this->setFactoryMock->expects($this->once())->method('create')->will($this->returnValue($attributeSetMock));
     $attributeSetMock->expects($this->once())->method('load')->with($productAttributeSetId)->willReturnSelf();
     //attribute set of wrong type
     $attributeSetMock->expects($this->once())->method('getEntityTypeId')->will($this->returnValue(3));
     $entityTypeMock->expects($this->once())->method('getId')->will($this->returnValue($productTypeId));
     $this->assertEquals($expectedErrorMessage, $this->model->validate($productMock));
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
 {
     $tierPrices = $product->getData('tier_price');
     try {
         $existingProduct = $this->get($product->getSku());
         $product->setData($this->resourceModel->getLinkField(), $existingProduct->getData($this->resourceModel->getLinkField()));
     } catch (NoSuchEntityException $e) {
         $existingProduct = null;
     }
     $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
     $productDataArray = array_replace($productDataArray, $product->getData());
     unset($productDataArray['media_gallery']);
     $ignoreLinksFlag = $product->getData('ignore_links_flag');
     $productLinks = null;
     if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
         $productLinks = $product->getProductLinks();
     }
     $productDataArray['store_id'] = (int) $this->storeManager->getStore()->getId();
     $product = $this->initializeProductData($productDataArray, empty($existingProduct));
     $this->processLinks($product, $productLinks);
     if (isset($productDataArray['media_gallery_entries'])) {
         $this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
     }
     if (!$product->getOptionsReadonly()) {
         $product->setCanSaveCustomOptions(true);
     }
     $validationResult = $this->resourceModel->validate($product);
     if (true !== $validationResult) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
     }
     try {
         if ($tierPrices !== null) {
             $product->setData('tier_price', $tierPrices);
         }
         unset($this->instances[$product->getSku()]);
         unset($this->instancesById[$product->getId()]);
         $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 (ValidatorException $e) {
         throw new CouldNotSaveException(__($e->getMessage()));
     } catch (LocalizedException $e) {
         throw $e;
     } 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());
 }
 /**
  * {@inheritdoc}
  */
 public function validate($object)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'validate');
     if (!$pluginInfo) {
         return parent::validate($object);
     } else {
         return $this->___callPlugins('validate', func_get_args(), $pluginInfo);
     }
 }