1. rewrite in config.xml:
<config>
<helpers>
<mysales>
<class>Bysoft_Mysales_Helper</class>
</mysales>
</helpers>
<frontend>
<routers>
<mysales>
<use>standard</use>
<args>
<module>Bysoft_Mysales</module>
<frontName>mysales</frontName>
</args>
</mysales>
<sales>
<args>
<modules>
<Bysoft_Mysales before="Mage_Sales">Bysoft_Mysales</Bysoft_Mysales>
</modules>
</args>
</sales>
</routers>
</frontend>
</config>
rewrite core sales order controller:
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Sales').DS.'OrderController.php';
class Bysoft_Mysales_OrderController extends Mage_Sales_OrderController {
public function cancelAction()
{
// Retrieve order_id passed by clicking on "Cancel Order" in customer account
$orderId = $this->getRequest()->getParam('id');
// Load Mage_Sales_Model_Order object
$order = Mage::getModel('sales/order')->load($orderId);
// Retrieve catalog session.
// We must use catalog session as customer session messages are not initiated for sales order view
// and this is where we want to redirect at the end of this action
// @see Mage_Sales_Controller_Abstract::_viewAction()
$session = Mage::getSingleton('catalog/session');
try {
// Make sure that the order can still be canceled since customer clicked on "Cancel Order"
if(!Mage::helper('mysales')->canCancel($order)) {
throw new Exception('Order cannot be canceled anymore.');
}
// Cancel and save the order
$order->cancel();
$order->save();
// If sending transactionnal email is enabled in system configuration, we send the email
if(Mage::getStoreConfigFlag('sales/cancel/send_email')) {
$order->sendOrderUpdateEmail();
}
$session->addSuccess($this->__('The order has been canceled.'));
}
catch (Exception $e) {
Mage::logException($e);
$session->addError($this->__('The order cannot be canceled.'));
}
// Redirect to current sale order view
$this->_redirect('sales/order/history');
}
}
Helper file for checking if the order can be canceled:
<?php
class Bysoft_Mysales_Helper_Data extends Mage_Core_Helper_Abstract
{
public function canCancel(Mage_Sales_Model_Order $order)
{
// If Magento decides that this order cannot be canceled
if(!$order->canCancel()) {
return false;
}
// Else... return true
return true;
}
}
Add link to template file:
<?php if (Mage::helper('mysales')->canCancel($_order)):?>
<span class="separator">|</span>
<a href="<?php echo $this->getUrl('sales/order/cancel', array('id'=>$_order->getId()));?>" class="link-cancel"><?php echo $this->__('Cancel') ?>
</a>
<?php endif;?>

本文介绍如何在Magento中定制订单取消功能,包括修改核心控制器、添加辅助类验证订单状态及前端模板显示等步骤。

被折叠的 条评论
为什么被折叠?



