Magento: How do I get observers to work in an external script?

核能气质少年 提交于 2019-12-02 21:02:33

My first guess would be the event you're trying to hook into is a <frontend /> or <admin /> event, because it looks like only <global /> 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 <global /> 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/line

File: 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 calling

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!