2

So I'm looking for an event for after a product is saved in the admin interface? Similar to catalog_product_prepare_save (which is before save).

It's important that it doesn't fire when e.g. another module does $product->save(). It should only fire when a someones saving a product manually using the admin interface.

Or any thoughts on other solutions of course.

7

There is no special event for saving the product from the admin interface, but you can use the general catalog_product_save_after and add a constraint to it based on the request.
Something like this:

public function catalogProductSaveAfter($observer) {
    if ($actionInstance = Mage::app()->getFrontController()->getAction()) {
        $action = $actionInstance->getFullActionName();
        if ($action == 'adminhtml_catalog_product_save') { //if on admin save action
            //do your magic here
        }
    }
}
| improve this answer | |
  • 1
    Will it not also work to define the event observer to listen via adminhtml in config.xml as well (as apart from global?) – ProxiBlue Oct 7 '14 at 15:00
  • 1
    @ProxiBlue. Yes and no. It will work on a clean install, but it will not work if you have other modules that call $product->save() from the backend. I mean the code in the event will be executed for those modules also. Using this verification it should be only executed in the admin product save page. – Marius Oct 7 '14 at 15:03
  • gotcha, as OP had requested, he was only interested in the admin panel product save, not the ->save() call on products in general across all admin areas - important bit of info I missed there ;) – ProxiBlue Oct 7 '14 at 15:10
  • But keep in mind that getAction() may or may not be an object, depending on where the event is triggered. – frigg Oct 9 '14 at 18:42
  • @frigg. Hmmm...that' may be possible. But can you give an example? – Marius Oct 9 '14 at 19:27
4

You could use controller_action_postdispatch_adminhtml_catalog_product_save, which is triggered after this action, but you should make sure that the product actually has been saved because the event will also be triggered if there was an error during save:

$product = Mage::registry('product');
if ($product && ! $product->hasDataChanges()) {

    // product has been saved

}

Explanation: _hasDataChanges is reset to false after a successful save.

| improve this answer | |
0

You can place catalog_product_save_after observer withing adminhtml tag of your module config.xml to make sure that will fire on product save from admin only

<?xml version="1.0"?>
<config>
    ...
    <adminhtml>
        ...
        <events>
            <catalog_product_save_after>
                <observers>
                    <product_save_after_observer>
                        <class>StackOverflow_CustomModule_Model_Observer</class>
                        <method>productSaveAfter</method>
                    </product_save_after_observer>
                </observers>
            </catalog_product_save_after>
        </events>
        ...
    </adminhtml>
    ...
</config>
| 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.