/**
  * {@inheritdoc}
  */
 public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
 {
     $product = $this->productRepository->get($sku);
     $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
     if ($existingMediaGalleryEntries == null) {
         throw new NoSuchEntityException(__('There is no image with provided ID.'));
     }
     $found = false;
     foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
         if ($existingEntry->getId() == $entry->getId()) {
             $found = true;
             $existingMediaGalleryEntries[$key] = $entry;
             break;
         }
     }
     if (!$found) {
         throw new NoSuchEntityException(__('There is no image with provided ID.'));
     }
     $product->setMediaGalleryEntries($existingMediaGalleryEntries);
     try {
         $this->productRepository->save($product);
     } catch (\Exception $exception) {
         throw new StateException(__('Cannot save product.'));
     }
     return true;
 }
Example #2
0
 /**
  * @param ProductAttributeMediaGalleryEntryInterface $entry
  * @return array
  */
 protected function convertFromMediaGalleryInterface(ProductAttributeMediaGalleryEntryInterface $entry)
 {
     $entryArray = ["value_id" => $entry->getId(), "file" => $entry->getFile(), "label" => $entry->getLabel(), "position" => $entry->getPosition(), "disabled" => $entry->isDisabled(), "types" => $entry->getTypes(), "content" => $this->convertFromMediaGalleryEntryContentInterface($entry->getContent())];
     return $entryArray;
 }
 /**
  * @param ProductAttributeMediaGalleryEntryInterface $entry
  * @return array
  */
 public function convertFrom(ProductAttributeMediaGalleryEntryInterface $entry)
 {
     $entryArray = ['value_id' => $entry->getId(), 'file' => $entry->getFile(), 'label' => $entry->getLabel(), 'position' => $entry->getPosition(), 'disabled' => $entry->isDisabled(), 'types' => $entry->getTypes(), 'media_type' => $entry->getMediaType(), 'content' => $this->convertFromMediaGalleryEntryContentInterface($entry->getContent())];
     return $entryArray;
 }
 /**
  * {@inheritdoc}
  */
 public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry, $storeId = 0)
 {
     try {
         $this->storeManager->getStore($storeId);
     } catch (\Exception $exception) {
         throw new NoSuchEntityException(__('There is no store with provided ID.'));
     }
     $product = $this->productRepository->get($sku);
     /** @var $productMediaGallery \Magento\Catalog\Model\Product\Attribute\Backend\Media */
     $productMediaGallery = $this->getGalleryAttributeBackend($product);
     $filePath = $this->entryResolver->getEntryFilePathById($product, $entry->getId());
     if ($filePath === null) {
         throw new NoSuchEntityException(__('There is no image with provided ID.'));
     }
     $productMediaGallery->updateImage($product, $filePath, ['label' => $entry->getLabel(), 'position' => $entry->getPosition(), 'disabled' => $entry->isDisabled()]);
     $productMediaGallery->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
     $productMediaGallery->setMediaAttribute($product, $entry->getTypes(), $filePath);
     $product->setStoreId($storeId);
     try {
         $this->productRepository->save($product);
     } catch (\Exception $exception) {
         throw new StateException(__('Cannot save product.'));
     }
     return true;
 }