Esempio n. 1
0
 /**
  * @param ProductInterface $product
  * @param array $mediaGalleryEntries
  * @return $this
  * @throws InputException
  * @throws StateException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
 {
     $existingMediaGallery = $product->getMediaGallery('images');
     $newEntries = [];
     if (!empty($existingMediaGallery)) {
         $entriesById = [];
         foreach ($mediaGalleryEntries as $entry) {
             if (isset($entry['id'])) {
                 $entry['value_id'] = $entry['id'];
                 $entriesById[$entry['value_id']] = $entry;
             } else {
                 $newEntries[] = $entry;
             }
         }
         foreach ($existingMediaGallery as $key => &$existingEntry) {
             if (isset($entriesById[$existingEntry['value_id']])) {
                 $updatedEntry = $entriesById[$existingEntry['value_id']];
                 $existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
             } else {
                 //set the removed flag
                 $existingEntry['removed'] = true;
             }
         }
         $product->setData('media_gallery', ["images" => $existingMediaGallery]);
     } else {
         $newEntries = $mediaGalleryEntries;
     }
     /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     $galleryAttributeBackend->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
     $images = $product->getMediaGallery('images');
     if ($images) {
         foreach ($images as $image) {
             if (!isset($image['removed']) && !empty($image['types'])) {
                 $galleryAttributeBackend->setMediaAttribute($product, $image['types'], $image['file']);
             }
         }
     }
     foreach ($newEntries as $newEntry) {
         if (!isset($newEntry['content'])) {
             throw new InputException(__('The image content is not valid.'));
         }
         /** @var ImageContentInterface $contentDataObject */
         $contentDataObject = $this->contentFactory->create()->setName($newEntry['content'][ImageContentInterface::NAME])->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])->setType($newEntry['content'][ImageContentInterface::TYPE]);
         $newEntry['content'] = $contentDataObject;
         $this->processNewMediaGalleryEntry($product, $newEntry);
     }
     return $this;
 }
 /**
  * {@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());
 }