除了强大的面向对象机制,比如重写或者继承来覆盖Magento本身的行为之外,还可以通过事件观察者模式来更新Magento固有的工作线路。
事件(Events)是发生在某一特定的地方的顺序流。打个比方说,客户下订单的顺序流:
1. 保存订单的详细信息
2. 发送订单的确认信息给客户
事件则可能在邮件的发出之前或者之后来引入自定义逻辑。
观察者(Observer)是一个事件的处理程序。它监听对应的事件,并且做出相应的反应。
使用Events + Observer 来覆盖行为类
简单地说,假设你需要彻底改变或者延长核心的逻辑,那么你的新逻辑将被重用或者覆盖现有的核心逻辑。如果你是对Magento提供的现有逻辑比较熟悉,并且需要添加新的核心逻辑的话,请使用事件。
举个例子,客户下单前,可以添加自己的附近信息。
Step 1 激活你的module
我们创建一个叫CustomerOrderComments的模块,并且以Yip来作为命名空间。
<?xml version="1.0"?>
<config>
<modules>
<Yip_CustomerOrderComments>
<active>true</active>
<codePool>local</codePool>
</Yip_CustomerOrderComments>
</modules>
</config>
Step 2 创建模块的配置文件
针对checkout_type_onepage_save_order事件,我们加入自己的观察者来进行监听,我们把observer类命名为Yip_CustomerOrderComments_Model_Observer
<?xml version="1.0"?>
<config>
<modules>
<Yip_CustomerOrderComments>
<version>0.1.0</version>
</Yip_CustomerOrderComments>
</modules>
<frontend>
<events>
<checkout_type_onepage_save_order>
<observers>
<Yip_CustomerOrderComments_Onepage>
<type>singleton</type>
<class>Yip_CustomerOrderComments_Model_Observer</class>
<method>setCustomerOrderComments</method>
</Yip_CustomerOrderComments_Onepage>
</observers>
</checkout_type_onepage_save_order>
</events>
</frontend>
</config>
Step 3 创建模版
在app\design\frontend\default\your_template\template\checkout\onepage下重新写agreements.phtml模版,这里主要是加入textarea元素,命名name为customerOrderComments,方便Observer接收。
<?php if (!$this->getAgreements()) return; ?>
<form action="" id="checkout-agreements" οnsubmit="return false;">
<ol class="checkout-agreements">
<?php foreach ($this->getAgreements() as $_a): ?>
<li>
<div class="agreement-content"<?php echo ($_a->getContentHeight() ? ' style="height:' . $_a->getContentHeight() . '"' : '')?>>
<?php if ($_a->getIsHtml()):?>
<?php echo $_a->getContent() ?>
<?php else:?>
<?php echo nl2br($this->htmlEscape($_a->getContent())) ?>
<?php endif; ?>
</div>
<p class="agree">
<input type="checkbox" id="agreement-<?php echo $_a->getId()?>" name="agreement[<?php echo $_a->getId()?>]" value="1" />
<label for="agreement-<?php echo $_a->getId()?>"><?php echo $_a->getCheckboxText()?></label>
</p>
</li>
<?php endforeach ?>
</ol>
<ol class="checkout-agreements">
<li>
<label for="customerOrderComments"><?php echo Mage::helper('core')->__('Other Quote Information') ?></label>
<br/><textarea name="customerOrderComments" id="customerOrderComments" style="width:450px;height:100px;"></textarea>
</li>
</ol>
</form>
Step 4 创建观察者类
class Yip_CustomerOrderComments_Model_Observer
{
public function setCustomerOrderComments(Varien_Event_Observer $observer)
{
$_order = $observer->getEvent()->getOrder();
$_request = Mage::app()->getRequest();
$_comments = strip_tags($_request->getParam('customerOrderComments'));
if(!empty($_comments)){
$_order->setCustomerNote('<br/><strong>Extra Instructions:</strong> ' .$_comments);
$_order->addStatusHistoryComment('<br/><strong>Extra Instructions:</strong> ' .$_comments);
//$_order->save();
}
return $this;
}
}
这里用getparam接收模版中的customerOrderComments,并且将其用setCustomerNote的方法来保存。OK,附上两个效果图:
前台下单前:
】
后台订单页:
That's all! Thanks~
转载请表明出处,谢谢!