1

I want to create a module that will hook events for images being added to products or vendors etc so that when it's saved and was already an existing image (image was being updated) - that it would invalidate that image (in cloudfront).

What events would allow me to detect this; maybe extend the image.php where it saves the image? Currently I don't see any dispatched events that will help me;

I tried: core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php:277:

Mage::dispatchEvent('catalog_product_media_add_image', array('product' => $product, 'image' => $file));

But found that the event is not dispatched when adding an image to a product. But technically - I only need to know about changed images - is there a way for me to detect (by hooking the before or after product save event) what properties have been updated?

Any direction would be greatly appreciated.

2

Try the event catalog_product_gallery_upload_image_after this should be fired on the upload action of the controller Mage_Adminhtml_Catalog_Product_GalleryController from your question I think this is the right event for you.

| improve this answer | |
  • 1
    is there any event for catalog image remove – Murtuza Zabuawala Nov 14 '16 at 12:19
  • @David Manners When I use the event catalog_product_gallery_upload_image_after I am not able to get the current product id. Could u help how to get current product id in the observer? – chris Oct 23 '18 at 10:39
1

First the credit https://stackoverflow.com/questions/8182009/get-product-changes

Now the solution. Hook into the catalog_product_save_before event and use the following observer to work out if images have changed.

class Custom_Module_Model_Observer {

public function handleProductSaveBefore(Varien_Event_Observer $observer)
{
    $product = $observer->getEvent()->getProduct();
    $diff = $this->_compareArrayAssocRecursive($product);

    if($this->hasImageChange($diff)) {
        //do stuff
    }

    return $observer;
}

protected function _compareArrayAssocRecursive($product)
{
    $diff = array();
    $attributes = $product->getTypeInstance(true)->getEditableAttributes($product);

    foreach ($attributes as $key => $value) {
        if ($product->dataHasChangedFor($key)) {
            $diff[$key] = $product->getData($key);
        }
    }

    return $diff;
}

public function hasImageChange($diff)
{
    $images = $diff['media_gallery']['images'];
    if($images) {
        $images = Zend_Json::decode($images);
        foreach($images as $image) {
            if(! $image['value_id'] || $image['removed']==1) {
                return true;
            }
        }
    }

    return false;
}

}

I thank you.

| 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.