0

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

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;

}

Now

$this->registry->register('isSecureArea', true);
$product = $this->product->load(2);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry) {
    $entry->setLabel('BlahBlah-1');
}
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
//$this->productRepository->save($product);
$product->save();
  • 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 – Bhavesh Tilvani Apr 15 '17 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 '17 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. – Bhavesh Tilvani Apr 15 '17 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. – Bhavesh Tilvani Apr 15 '17 at 8:49
  • Check updated answer. – Sohel Rana Apr 15 '17 at 8:59

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.