1

i create simple module, which auto product name in images gallery alt tag

Evern :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

        <event name="catalog_product_save_after">
            <observer name="imagesalttags_catalog_product_save_after" instance="Vendor\Module\Observer\CatalogProductSaveAfter" />
        </event>

    </config>

call observer :

<?php

namespace Vendor\Module\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;

use Magento\Store\Model\StoreManagerInterface;

class CatalogProductSaveAfter implements ObserverInterface
{

    protected $scopeConfigObject;

    protected $_storeManager;

    protected $_request;

    public function __construct(
        \Magento\Catalog\Helper\Product $catalogProductHelper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigObject,
        \Magento\Framework\App\RequestInterface $request,
        StoreManagerInterface $storeManager
    ) {
        $this->catalogProductHelper = $catalogProductHelper;
        $this->scopeConfigObject = $scopeConfigObject;
        $this->_request = $request;
        $this->_storeManager = $storeManager;


    }

    public function execute(Observer $observer)
    {   

        $product = $observer->getProduct();

        $pname = $product->getName();

        $existingMediaGalleryEntries = $product->getMediaGalleryEntries();

        foreach ($existingMediaGalleryEntries as $key => $entry) {

                    $entry->setLabel($pname);


                }
                $product->setMediaGalleryEntries($existingMediaGalleryEntries);
                $product->save();   


        return ;
    }
}

But product save is not working, continue loader is display. Please can you help it any one know how to save product data via observer for above case

Your Answer

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

Browse other questions tagged or ask your own question.