在magento中,我们需要给用户中心的订单栏目中每个订单后面添加一个取消订单的按钮(如果这个订单可以取消),那么下面我们来新建一个模块,来开发这个取消订单的功能。首先写好模块基本配置。如:“webroot/app/etc/modules/Magease_Cancelorder.xml”,在这里我们需要让我们的模块depends magento的sales模块。在代码里面添加depends标签,如下:
<?xml version="1.0" ?>
<config>
<modules>
<Magease_Cancelorder>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Sales />
</depends>
</Magease_Cancelorder>
</modules>
</config>
然后我们来创建模块配置文件,“webroot/app/code/community/magease/cancelorder/etc/config.xml”,如:
<?xml version="1.0"?>
<config>
<modules>
<Magease_Cancelorder>
<version>0.1.0</version>
</Magease_Cancelorder>
</modules>
</config>
声明我们的模块controller,在这里,我们需要来重写magento的controller(具体重写规则,请参考magento rewrite 博客),如:
<frontend>
<routers>
<sales>
<args>
<modules>
<Magease_Cancelorder before="Mage_Sales">Magease_Cancelorder</Magease_Cancelorder>
</modules>
</args>
</sales>
</routers>
</frontend>
并在控制器中添加canceAction()和recentCancelAction()方法。如:
"webroot/app/code/community/magease/cancelorder/controllers/OrderController.php"
<?php
require_once 'Mage'.DS.'Sales'.DS.'controllers'.DS.'OrderController.php';
class Magease_Cancelorder_OrderController extends Mage_Sales_OrderController {
protected function _cancelAction(){
if (!$this->_loadValidOrder()) {
return;
}
$order = Mage::registry('current_order');
if($order->canCancel()){
$order->cancel();
$order->setStatus('canceled');
$order->save();
$order->sendOrderUpdateEmail();
}
$session = Mage::getSingleton('core/session');
$session->addSuccess('The order has been canceld.');
}
public function cancelAction(){
$this->_cancelAction();
$this->_redirect('*/*/history');
}
public function recentcancelAction(){
$this->_cancelAction();
$this->_redirect('customer/account');
}
}
之后,我们需要重写blocks,用来在模版文件中添加cancel和recentCancel这两个方法。如:
<global>
<blocks>
<sales>
<rewrite>
<order_recent>Magease_Cancelorder_Block_Order_Recent</order_recent>
<order_history>Magease_Cancelorder_Block_Order_History</order_history>
</rewrite>
</sales>
</blocks>
</global>
写好之后,我们来声明一下布局文件,用来设置模版文件。如:
<layout>
<updates>
<cancelorder>
<file>cancelorder.xml</file>
</cancelorder>
</updates>
</layout>
然后去布局文件中,写布局。如:
<?xml version="1.0"?>
<layout>
<customer_account_index>
<reference name="customer_account_dashboard_top">
<action method="setTemplate">
<template>cancelorder/sales/order/recent.phtml</template>
</action>
</reference>
</customer_account_index>
<sales_order_history>
<reference name="sales.order.history">
<action method="setTemplate">
<template>cancelorder/sales/order/history.phtml</template>
</action>
</reference>
</sales_order_history>
</layout>
最后,复制当前模版的sales/order/history.phtml和sales/order/recent.phtml这两个文件到布局声明的路径中,并且在
<?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
<span class="separator">|</span> <a href="<?php echo $this->getReorderUrl($_order) ?>" class="link-reorder"><?php echo $this->__('Reorder') ?></a>
<?php endif ?>
之后添加如下代码:
<?php if ($_order->canCancel()) : ?>
<span class="separator">|</span> <a href="<?php echo $this->getCancelUrl($_order) ?>"><?php echo $this->__('Cancel') ?></a>
<?php endif ?>
到此,我们的订单取消按钮就已经加上了,经测试,以上模块在1.7,1.8,1.9中均可正常运行,如有其他问题,请留言。