本文整理汇总了PHP中Magento\Catalog\Api\Data\ProductInterface::getMediaGallery方法的典型用法代码示例。如果您正苦于以下问题:PHP ProductInterface::getMediaGallery方法的具体用法?PHP ProductInterface::getMediaGallery怎么用?PHP ProductInterface::getMediaGallery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Api\Data\ProductInterface
的用法示例。
在下文中一共展示了ProductInterface::getMediaGallery方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processMediaGallery
/**
* @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;
}