1

I have a grouped with some associated products. When the image on the grouped is updated, I'd like to save the same image on the associated products also.

I tried with the "catalog_product_gallery_upload_image_after" observer: this observer gets trigged on image change but it doesn't give me any info on the product that has been saved.

I also tried with the more generic "product_save" event (after and before): here I have the product data but I cannot fetch the image change.

Is there a way to trigger my code on the image change in a way that I can intercept the image change with the data of the product that has been saved?

Thanks

1 Answer 1

0

Give a try on this event - catalog_product_save_commit_after.

It will give updated image data as well.

<event name="catalog_product_save_commit_after">
        <observer name="some_random_event_name" instance="Vendor\Module\Model\Observer\ImageTransfer"/> 
</event>

Sample code for Vendor\Module\Model\Observer\ImageTransfer.php

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $catalogProduct = $observer->getEvent()->getProduct();
        if (!$catalogProduct instanceof \Magento\Catalog\Model\Product) {
            return;
        }

        $image = $observer->getEvent()->getProduct()->getImage();
        $thumbnail = $observer->getEvent()->getProduct()->getThumbnail();
        $small_image = $observer->getEvent()->getProduct()->getSmallImage();

        if (($image != "no_selection") && (!empty($image))) {
            //do with $image
        }
        if (($thumbnail != "no_selection") && (!empty($thumbnail))) {
            //do with $thumbnail
        }
        if (($small_image != "no_selection") && (!empty($small_image))) {
            //do with $small_image
        }
    }

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.