Created
December 3, 2013 07:41
-
-
Save anunay/7765442 to your computer and use it in GitHub Desktop.
Magento: Get Observers To Work In An External Script?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Magento: Get Observers To Work In An External Script? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Magento: Get Observers To Work In An External Script?
When a script is run outside of Magento, observers are not invoked when an event is fired. Why? How do I fix it?
Below is the original issue that lead me to this question. The issue is that the observer that would apply the catalog rule is never called. The event fires, but the observer doesn't pick it up.
I'm running an external script that loads up a Magento session.
Within that script, I'm loading products and grabbing a bunch of properties. The one issue is thatgetFinalPrice() does not apply the catalog rules that apply to the product.
I'm doing everything I know to set the session, even a bunch of stuff that I think is superfluous. Nothing seems to get these rules applied.
Here's a test script:
Any insight is appreciated.
Answer
The event you're trying to hook into is a
<frontend />
or<admin />
event, because it looks like only events fire when you run a command line script.Magento has this concept called "areas". Areas are sort-of like individual applications that live in the system (but not quite, I'm still a little fuzzy on the concept).When you setup a config.xml with your observers, you're either placing them in a
<global />
tag, a<frontend />
tag, or a<admin />
tag.When it comes to events, Magento only loads up areas that it has to deal with for a particualr request. So, the area always gets loaded. However, the
<frontend />
or<admin />
areas only get loaded up if the application gets to the controller dispatch. Specifcally, in the following file/lineFile:
app/code/core/Mage/Core/Controller/Varien/Action.php Mage::app()->loadArea($this->getLayout()->getArea());
That never happens with a command line application. Only the
<global />
area gets loaded.So, as mentioned in the first paragraph, my guess is your observer isn't firing because Magento never loads the
<frontend />
area of the application. As for solutions, you could try moving your observer to the<global />
area. You could also try manually callingMage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
although, you'd be loading ALL observers in the
<frontend />
area, many of which have probably been created assuming a web browser context.If none of that helps, take a look at the dispatchEvent method on the
Mage_Core_Model_App
class. That's where event observers get called.