Magento 中的自定义事件 [英] Custom Events in Magento
问题描述
如何在 Magento 中添加自定义事件,例如done_some_thing"?
How can I add a custom event in Magento like "done_some_thing"?
我正在编写一个购物车,它为通过特定链接访问的客户提供一定的折扣,并希望在购物车和结帐页面中显示相同的内容
I am coding for a shopping cart which gives a certain % discount for the customers who comes through a particular link, and want to show the same in both Cart and Checkout Page
推荐答案
要调度事件,请使用 Mage::dispatchEvent
函数(%magento%/app/Mage.php around第 425 行
).调用看起来像这样:
To dispatch an event, use the Mage::dispatchEvent
function (%magento%/app/Mage.php around line 425
). Calls look like this:
$data = array( 'somedata' => 'foo', 'layout' => $this->getLayout());
Mage::dispatchEvent('my_event_name', $data);
为了观察事件,请在扩展的 config.xml 文件中指定观察者.
In order to observe an event, specify the observer in the config.xml file of your extension.
<config>
<global>
<events>
<my_event_name>
<observers>
<myextension>
<type>singleton</type>
<class>myextension/observer</class>
<method>someMethodName</method>
</myextension>
</observers>
</my_event_name>
</events>
</global>
</config>
创建一个相应的类和方法(Observer.php),你就可以开始了:
Create a corresponding class and method (Observer.php) and you're set to go:
class Myextension_Model_Observer {
public function someMethodName($event) {
$layout = $event->getLayout();
$someData = $event->getSomedata();
}
}
这篇关于Magento 中的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!