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

查看:19
本文介绍了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();

感谢任何见解.

推荐答案

我的第一个猜测是您尝试连接的事件是 <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 时,您要么将它们放在 标记、 标记或 标记.

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 只会加载它必须为特定请求处理的区域.所以, 区域总是被加载.然而, 区域只有在应用程序到达控制器调度时才会加载.具体来说,在以下文件/行中

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.

因此,如第一段所述,我的猜测是您的观察者不会触发,因为 Magento 从未加载应用程序的 区域.至于解决方案,您可以尝试将您的观察者移动到 <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/> 区域加载所有观察者,其中许多可能是在假设 Web 浏览器上下文的情况下创建的.

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

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

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天全站免登陆