Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273276
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-03-04 01:55:26

在Magento的用户面板中,并没有取消订单的功能,为了使用户可以取消订单,可以新建一个模块,这里将使用MagentoBoy_Example作为例子。

1 建立模块文件:
/app/etc/modules/MagentoBoy_Example.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_Example>
  5.             <active>true</active>
  6.             <codePool>local</codePool>
  7.             <depends>
  8.                 <Mage_Sales />
  9.             </depends>
  10.         </MagentoBoy_Example>
  11.     </modules>
  12. </config>
并添加配置文件:
/app/code/local/MagentoBoy/Example/etc/config.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_Example>
  5.             <version>0.1.0</version>
  6.         </MagentoBoy_Example>
  7.     </modules>
  8. </config>

2 扩展Mage_Sales_OrderController控制器
因为在Account Dashboard和My Orders页面都会显示Order表格,需要扩展Mage_Sales_OrderController控制器,添加cancel和recentCancel方法:
/app/code/local/MagentoBoy/Example/controllers/OrderController.php
  1. <?php

  2. require_once 'Mage/Sales/controllers/OrderController.php';

  3. class MagentoBoy_Example_OrderController extends Mage_Sales_OrderController
  4. {
  5.     protected function _cancelAction()
  6.     {
  7.         if (!$this->_loadValidOrder()) {
  8.             return;
  9.         }
  10.         $order = Mage::registry('current_order');

  11.         if ($order->canCancel())
  12.         {
  13.             $order->cancel();
  14.             $order->setStatus('canceled');
  15.             $order->save();
  16.             $order->sendOrderUpdateEmail();
  17.         }

  18.         $session = Mage::getSingleton('core/session');
  19.         $session->addSuccess('The order has been canceld.');
  20.     }

  21.     public function cancelAction()
  22.     {
  23.         $this->_cancelAction();
  24.         $this->_redirect('*/*/history');
  25.     }

  26.     public function recentCancelAction()
  27.     {
  28.         $this->_cancelAction();
  29.         $this->_redirect('customer/account');
  30.     }
  31. }
并修改配置文件config.xml重写控制器:
  1. <config>
  2.     <frontend>
  3.         <routers>
  4.             <sales>
  5.                 <args>
  6.                     <modules>
  7.                         <MagentoBoy_Example before="Mage_Sales">MagentoBoy_Example</MagentoBoy_Example>
  8.                     </modules>
  9.                 </args>
  10.             </sales>
  11.         </routers>
  12.     </frontend>
  13. </config>

3 扩展Mage_Sales_Block_Order_Recent和Mage_Sales_Block_Order_History
为了在Order页面中添加取消订单的链接,需要扩展这两个Block,添加getCancelUrl()函数:
/app/code/local/MagentoBoy/Example/Block/Order/Recent.php
  1. <?php

  2. class MagentoBoy_Example_Block_Order_Recent extends Mage_Sales_Block_Order_Recent
  3. {
  4.     public function getCancelUrl($order)
  5.     {
  6.         return $this->getUrl('sales/order/recentcancel', array('order_id' => $order->getId()));
  7.     }
  8. }

/app/code/local/MagentoBoy/Example/Block/Order/History.php
  1. <?php

  2. class MagentoBoy_Example_Block_Order_History extends Mage_Sales_Block_Order_History
  3. {
  4.     public function getCancelUrl($order)
  5.     {
  6.         return $this->getUrl('*/*/cancel', array('order_id' => $order->getId()));
  7.     }
  8. }
并修改配置文件config.xml重写Block
  1. <config>
  2.     <global>
  3.         <blocks>
  4.             <sales>
  5.                 <rewrite>
  6.                     <order_recent>MagentoBoy_Example_Block_Order_Recent</order_recent>
  7.                     <order_history>MagentoBoy_Example_Block_Order_History</order_history>
  8.                 </rewrite>
  9.             </sales>
  10.         </blocks>
  11.     </global>
  12. </config>

4 添加Layout文件
/app/design/frontend/default/default/layout/example.xml
  1. <?xml version="1.0"?>
  2. <layout>
  3.     <customer_account_index>
  4.         <reference name="customer_account_dashboard_top">
  5.             <action method="setTemplate">
  6.                 <template>example/order/recent.phtml</template>
  7.             </action>
  8.         </reference>
  9.     </customer_account_index>
  10.     
  11.     <sales_order_history>
  12.         <reference name="sales.order.history">
  13.             <action method="setTemplate">
  14.                 <template>example/order/history.phtml</template>
  15.             </action>
  16.         </reference>
  17.     </sales_order_history>
  18. </layout>
并修改配置文件config.xml添加layout文件
  1. <config>
  2.     <frontend>
  3.         <layout>
  4.             <updates>
  5.                 <example>
  6.                     <file>example.xml</file>
  7.                 </example>
  8.             </updates>
  9.         </layout>
  10.     </frontend>
  11. </config>

5 修改模板文件
复制/app/design/frontend/base/default/template/sales/order目录下的recent.phtml和history.phtml到
/app/design/frontend/default/default/template/order目录
修改recent.phtml和history.phtml
  1. <?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
  2.     <span class="separator">|</span> <a href="getReorderUrl($_order) ?>" class="link-reorder"><?php echo $this->__('Reorder') ?></a>
  3. <?php endif ?>
下面添加
  1. <?php if ($_order->canCancel()) : ?>
  2.     <span class="separator">|</span> <a href="getCancelUrl($_order) ?>"><?php echo $this->__('Cancel') ?></a>
  3. <?php endif ?>

刷新缓存,可以看到订单页面可以取消的订单显示了cancel的链接,点击后将取消此订单。


阅读(3814) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~