Commit 887c45c5 by liumengfei

优化批量上传图片接口

parent 9bdab0d2
...@@ -43,6 +43,22 @@ interface ProductAttributeMediaGalleryManagementInterface ...@@ -43,6 +43,22 @@ interface ProductAttributeMediaGalleryManagementInterface
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
); );
/**
* Update gallery entry
*
* @param string $sku
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function update(
$sku,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
);
/** /**
* Retrieve the list of gallery entries associated with given product * Retrieve the list of gallery entries associated with given product
* *
......
...@@ -162,6 +162,49 @@ class GalleryManagement implements \Joshine\Catalog\Api\ProductAttributeMediaGal ...@@ -162,6 +162,49 @@ class GalleryManagement implements \Joshine\Catalog\Api\ProductAttributeMediaGal
throw new StateException(__('The new media gallery entry failed to save.')); throw new StateException(__('The new media gallery entry failed to save.'));
} }
/**
* @inheritdoc
*/
public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
{
$product = $this->productRepository->get($sku, true);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
if ($existingMediaGalleryEntries == null) {
throw new NoSuchEntityException(
__('No image with the provided ID was found. Verify the ID and try again.')
);
}
$found = false;
$entryTypes = (array)$entry->getTypes();
foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
$existingEntryTypes = (array)$existingEntry->getTypes();
$existingEntry->setTypes(array_diff($existingEntryTypes, $entryTypes));
if ($existingEntry->getId() == $entry->getId()) {
$found = true;
$existingMediaGalleryEntries[$key] = $entry;
}
}
if (!$found) {
throw new NoSuchEntityException(
__('No image with the provided ID was found. Verify the ID and try again.')
);
}
$product = $this->productInterfaceFactory->create();
$product->setSku($sku);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new StateException(__("The product can't be saved."));
}
return true;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment