2

I'm using the observer called catalog_product_save_after to track changes to a product. Whenever this observer fires, I don't have access to the store context. I would like to get only the store contexts that the product has been modified in.

How can I get only the stores that a product has been modified for in the catalog_product_save_after event observer?

6
+50

A product can only be saved for one store at a time. So when saving a product, you are always in the context of a single store. The store id can be retrieved from the Varien_Event_Observer observer argument as follows:

$observer->getEvent()->getProduct()->getStoreId()

Note that saving an attribute value for a product in one store can have an impact on attribute value of the corresponding attribute for the same product in another store.

For instance, consider saving a product with id 123 for store 0(the admin store), and in which the attribute name (with attribute id 71) has been changed. When loading product id 123 again for store 1 (the default frontend store), the value of attribute name might also be updated. You can check this as follows:

  1. In the admin, go to the product edit page, and switch to store 1 using the configuration scope box. If the "Use default" checkbox is not checked, the value from store 1 is shown. Otherwise, the value from store 0 is shown.

  2. In the database, you can see this in the corresponding product attribute table, in this case catalog_product_entity_varchar. If there is no value for the combination of store id 1, entity id 123 and attribute id 71, then the value for the combination of store id 0, entity id 123 and attribute id 71 is taken.

1

Not sure how you actually modify the product, but if you modify it via the admin's product edit page, you should be able to get the store ID via:

$observer->getProduct()->getStoreId()

This gives you the store ID of the store in which the product has been saved (the store which is selected in the upper left store switcher).

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.