Magento:如何让观察者在外部脚本中工作?

发布于 2024-08-31 21:06:56 字数 786 浏览 4 评论 0原文

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

以下是导致我提出这个问题的原始问题。问题是应用目录规则的观察者永远不会被调用。事件触发,但观察者没有接收到它。


我正在运行一个加载 Magento 会话的外部脚本。

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

我正在尽我所知道的一切来设置会话,甚至包括一些我认为多余的东西。似乎没有什么可以应用这些规则。

这是一个测试脚本:

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

任何见解都值得赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-09-07 21:06:56

我的第一个猜测是您尝试挂钩的事件是 事件,因为它看起来只是当您运行命令行脚本时, 事件会触发。

Magento 有一个称为“区域”的概念。区域有点像系统中的单个应用程序(但不完全是,我对这个概念仍然有点模糊)。当您使用观察者设置 config.xml 时,您可以将它们放在 < code>标记、 标记或 标记。

当涉及到事件时,Magento 仅加载它必须处理特定请求的区域。因此, 区域始终会被加载。但是,只有当应用程序到达控制器调度时, 区域才会加载。具体来说,在以下文件/行中,

File: app/code/core/Mage/Core/Controller/Varien/Action.php
Mage::app()->loadArea($this->getLayout()->getArea());

命令行应用程序永远不会发生这种情况。仅加载 区域。

因此,正如第一段中提到的,我的猜测是您的观察者没有触发,因为 Magento 从不加载应用程序的 区域。至于解决方案,您可以尝试将观察者移至 区域。您也可以尝试手动调用

Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

,但您将在 区域加载所有观察者,其中许多观察者可能是在假设 Web 浏览器上下文的情况下创建的。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文