I am saving the product programmatically using action class in magento 2 [Reference link].After saving the product i am saving the images.But while saving the labels of the images i am getting the following error...

Fatal error: Call to undefined method Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend\Interceptor::updateImage()

I have used the following code for saving the image label after product save...

$_product = $this->objectManager->get('Magento\Catalog\Model\Product')->load($_product->getId()); //$_product->getId() is 5 which is id of product saved 
            $attributes = $_product->getTypeInstance(true)->getSetAttributes($_product);
            $gallery = $attributes['media_gallery'];
            //$gallery = $_product->getData('media_gallery');
            $images = $_product->getMediaGalleryImages();
            foreach ($images as $image) {
                $backend = $gallery->getBackend();
                $backend->updateImage($_product,$image->getFile(),array('label' => 'Blah'));

Referred above code from this link and made changes...

Please suggest how to use updateImage function in magento 2 and save labels for images after saving the product...

2 Answers 2


Try following way:

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Catalog\Model\Product $product,
    \Magento\Framework\Registry $registry
) {
    $this->productRepository = $productRepository;
    $this->product = $product;
    $this->registry = $registry;



$this->registry->register('isSecureArea', true);
$product = $this->product->load(2);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry) {
  • The value is added in the 'catalog_product_entity_media_gallery_value' table but it is not showing as alt text of the product images in the admin panel Apr 15, 2017 at 7:34
  • Load correct product, then It should change accordingly. May be you load store wise product, that's why it's create another entry for image into catalog_product_entity_media_gallery_value table.
    – Sohel Rana
    Apr 15, 2017 at 8:02
  • I have tried with new product save from front end..it saved with id no 6,but when i saw the product in admin panel after opening it is showing images but with no caption. Apr 15, 2017 at 8:43
  • It is showing when i select default store view from the top while editing the product...when by natural flow directly clicking on product edit it is not showing while product opens for edit mode. Apr 15, 2017 at 8:49
  • Check updated answer.
    – Sohel Rana
    Apr 15, 2017 at 8:59

On M2 2.3.3 I needed to use




Only then could I save images with the hidden field set or the labels. My code was actually saving before but only to one of the stores. Needless to say I assumed Magento was broken and didn't check any of the store views.

To use storeManager you will be needing:

use Magento\Store\Model\StoreManagerInterface;

private $storeManager;

public function __construct(
    StoreManagerInterface $storeManager,
) {
    $this->storeManager = $storeManager;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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