Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 891
Laizer Avatar asked May 12 '10 09:05

Laizer


People also ask

How do I add an observer in Magento 2?

Magento 2 Observers To create an observer in Magento 2, you must place your class file under the ModuleRoot/Observer directory. The observer class file should use Magento\Framework\Event\Observer and Magento\Framework\Event\ObserverInterface class and define the executive function.

In which order the observers will get executed which are listening to the same event?

Observer will be executed in all areas: adminhtml , crontab , frontend , graphql , webapi_rest , webapi_soap . Observer will be executed in the adminhtml area only. Observer will be executed in the crontab area only.

How do I use event observer in Magento 2?

Working with events and observers is one of the main ways to extend Magento functionality. The events and observers implementation in Magento 2 is based on the publish-subscribe pattern. Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event.


1 Answers

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.

like image 92
Alan Storm Avatar answered Oct 10 '22 06:10

Alan Storm