Magento:如何让观察者在外部脚本中工作? [英] Magento: How do I get observers to work in an external script?

查看:194
本文介绍了Magento:如何让观察者在外部脚本中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当脚本在Magento之外运行时,在触发事件时不会调用观察者。为什么?如何解决?

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.

我正在运行加载的外部脚本Magento会议。

I'm running an external script that loads up a Magento session.

在该脚本中,我正在加载产品并获取一堆属性。一个问题是 getFinalPrice()不适用于产品的目录规则。

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.

这是一个测试脚本:

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();

任何见解都表示赞赏。

推荐答案

我的第一个猜测是您尝试挂钩的事件是一个< frontend /> < admin /> 事件,因为当您运行命令行脚本时,它似乎只是< global /> 事件触发。

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有这个概念叫做区域。区域是像系统中的个别应用程序(但不完全相同,我仍然有点模糊的概念)。当您与观察者设置config.xml时,您将它们放在一个< global /> 标签,< frontend /> 标签或 < admin /> 标签。

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.

当涉及到事件时,Magento只会加载必须处理特定请求的区域。所以,< global /> 区域总是被加载。但是,如果应用程序中的< frontend /> < admin /> 得到控制器调度。具体来说,在以下文件/行中

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());

从来没有发生过命令行应用程序。只有< global /> 区域被加载。

That never happens with a command line application. Only the <global /> area gets loaded.

所以,如第一段所述,我的猜测是你的观察者不是射击,因为Magento从不加载< frontend /> 应用程序的区域。对于解决方案,您可以尝试将观察者移动到< global /> 区域。您还可以尝试手动调用

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);

尽管您将加载所有观察者在< frontend / < / code>区域,其中许多可能是假设Web浏览器上下文创建的。

although, you'd be loading ALL observers in the <frontend /> area, many of which have probably been created assuming a web browser context.

如果没有帮助,请查看 dispatchEvent 方法在 Mage_Core_Model_App 类。这就是事件观察者被调用的地方。

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.

这篇关于Magento:如何让观察者在外部脚本中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆