2

I am writing an import product job on Magento 2. I want to delete all product images if I need to update the product. I try to use $product->getMediaGalleryImages() to get all the images from the product but it returns null after running. I ensure that the product has an image. Follows is the code I tried.

$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product = $product->getCollection()->addFieldToSelect("*")->addAttributeToFilter('sku', 'sku-01')->getFirstItem();
$images = $product->getMediaGalleryImages(); // it return null

And follows is the code to upload product image.

$product->setMediaGallery(array('images' => array(), 'values' => array()))
                            ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);
4

You can get the product media gallery images as given below.

Method 1: Use dependency. (Recommended)

Create a helper

app/code/Vendor/Module/Helper/Data.php

<?php

namespace Vendor\Module\Helper;

use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $galleryReadHandler;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        GalleryReadHandler $galleryReadHandler
    )
    {
        $this->galleryReadHandler = $galleryReadHandler;
        parent::__construct($context);
    }

    /** Add image gallery to $product */
    public function addGallery($product) {
        $this->galleryReadHandler->execute($product);
    }

}

Now add code to your phtml file as below.

$_helperGallery = $this->helper('Vendor\Module\Helper\Data');
$_helperGallery->addGallery($product);
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    print_r($image->getData());
}

Method 2: Use objectManager directly in your phtml file. (Not Recommended)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get("Magento\Catalog\Model\Product\Gallery\ReadHandler")->execute($product);
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    print_r($image->getData());
}
| improve this answer | |
3
/* Add media gallery data to loaded items
 * @since 101.0.1
 * addMediaGalleryData()
 */
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product = $product->getCollection()
->addFieldToSelect("*")
->addAttributeToFilter('sku', 'sku-01')
->addMediaGalleryData()
->getFirstItem();
$images = $product->getMediaGalleryImages();

Try to add addMediaGalleryData() to your collection. Without it, getMediaGalleryImages() will always return null

| improve this answer | |
1

Your issue may be with this code

$product = $product->getCollection()->addFieldToSelect("*")->addAttributeToFilter('sku', 'sku-01')->getFirstItem();

Try below code instead

$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product = $product->loadByAttribute('sku', 'sku-01');
$images = $product->getMediaGalleryImages(); 

Note:- you should not use object manager directly instead use ProductRepository check this answer for proper Magento 2 way to load product

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.