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());
    }
 /**
  * {@inheritdoc}
  */
 public function get($sku, $editMode = false, $storeId = null, $forceReload = false)
 {
     $cacheKey = $this->getCacheKey([$editMode, $storeId]);
     if (!isset($this->instances[$sku][$cacheKey]) || $forceReload) {
         $product = $this->productFactory->create();
         $productId = $this->resourceModel->getIdBySku($sku);
         if (!$productId) {
             throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
         }
         if ($editMode) {
             $product->setData('_edit_mode', true);
         }
         if ($storeId !== null) {
             $product->setData('store_id', $storeId);
         }
         $product->load($productId);
         $this->instances[$sku][$cacheKey] = $product;
         $this->instancesById[$product->getId()][$cacheKey] = $product;
     }
     return $this->instances[$sku][$cacheKey];
 }
 /**
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function executeImportReplaceTest($skus, $skippedAttributes)
 {
     $replacedAttributes = ['row_id', 'entity_id', 'tier_price', 'is_salable', 'media_gallery'];
     $skippedAttributes = array_merge($replacedAttributes, $skippedAttributes);
     $index = 0;
     $ids = [];
     $origProducts = [];
     while (isset($skus[$index])) {
         $ids[$index] = $this->productResource->getIdBySku($skus[$index]);
         $origProducts[$index] = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load($ids[$index]);
         $index++;
     }
     $csvfile = $this->exportProducts();
     $this->importProducts($csvfile, \Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE);
     while ($index > 0) {
         $index--;
         $id = $this->productResource->getIdBySku($skus[$index]);
         $newProduct = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load($id);
         // check original product is deleted
         $origProduct = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load($ids[$index]);
         $this->assertNull($origProduct->getId());
         // check new product data
         // @todo uncomment or remove after MAGETWO-49806 resolved
         //$this->assertEquals(count($origProductData[$index]), count($newProductData));
         $origProductData = $origProducts[$index]->getData();
         $newProductData = $newProduct->getData();
         $this->assertEqualsOtherThanSkippedAttributes($origProductData, $newProductData, $skippedAttributes);
         $this->assertEqualsSpecificAttributes($origProducts[$index], $newProduct);
         foreach ($replacedAttributes as $attribute) {
             if (isset($origProductData[$attribute])) {
                 $expected = is_array($origProductData[$attribute]) ? array_filter($origProductData[$attribute]) : $origProductData[$attribute];
                 if (!empty($expected)) {
                     $actual = isset($newProductData[$attribute]) ? $newProductData[$attribute] : null;
                     $actual = is_array($actual) ? array_filter($actual) : $actual;
                     $this->assertNotEquals($expected, $actual, $attribute . ' is expected to be changed');
                 }
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getIdBySku($sku)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getIdBySku');
     if (!$pluginInfo) {
         return parent::getIdBySku($sku);
     } else {
         return $this->___callPlugins('getIdBySku', func_get_args(), $pluginInfo);
     }
 }