tags:

views:

191

answers:

1

As far as I can tell, 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 that getFinalPrice() 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:

require_once "app/Mage.php";
umask(0);
$app = Mage::app("default");

$app->getTranslator()->init('frontend');  //Probably not needed
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton("customer/session");
$session->start();  //Probably not needed
$session->loginById(122);

$product = Mage::getModel('catalog/product')->load(1429);
echo $product->getFinalPrice();

Any insight is appreciated.

+2  A: 

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.

Alan Storm
Another solid lesson in Magento architecture. Thanks, Alan.
Laizer
Stellar answer Alan. Thanks for the info :)
Joseph Mastey