我使用以下代码来阻止从Magento管理面板取消订单。
<?xml version="1.0"?>
<config>
<modules>
<Muk_OrderCancel>
<version>1.0.0</version>
</Muk_OrderCancel>
</modules>
<global>
<models>
<ordercancel>
<class>Muk_OrderCancel_Model</class>
</ordercancel>
</models>
<events>
<sales_order_save_before>
<observers>
<ordercancel>
<type>singleton</type>
<class>Muk_OrderCancel_Model_Observer</class>
<method>canCancelOrder</method>
</ordercancel>
</observers>
</sales_order_save_before>
</events>
<helpers>
<ordercancel>
<class>Muk_OrderCancel_Model_Helper</class>
</ordercancel>
</helpers>
</global>
</config>
在观察者中我使用以下代码:
<?php
class Muk_OrderCancel_Model_Observer
{
public function canCancelOrder( Varien_Event_Observer $observer )
{
$incrementId = $observer->getEvent()->getOrder()->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
$adminUserSession = Mage::getSingleton('admin/session');
$adminUserId = $adminUserSession->getUser()->getUserId();
$adminUserName = $adminUserSession->getUser()->getUsername();
$adminRoleName = Mage::getModel('admin/user')->load($adminUserId)
->getRole()->getData('role_name');
if($adminRoleName) { //some condition
$order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, false);
}
}
}
但即使在启用此模块后,订单也会被取消。
如何阻止取消订单?
答案 0 :(得分:2)
在&#34; Mage_Adminhtml_Sales_OrderController&#34; &#34; cancelAction&#34;它会:
$order->cancel()
->save();
这意味着首先取消订单然后你的观察员开火。虽然我发现了这个事件:
Mage::dispatchEvent('sales_order_payment_cancel', array('payment' => $this));
in&#34; Mage_Sales_Model_Order_Payment&#34;之前发生过:&#34; Mage_Sales_Model_Order&#34; :&#34; registerCancellation&#34;方法
在你的观察者方法中触发此事件你可以这样做:
if ($adminRoleName) {
$payment = $observer->getEvent()->getPayment();
$order = $payment->getOrder();
$order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, false);
//Get the existing non cancelled orders if they exist, if not create the array and add it to the admin session.
$orderIds = Mage::getSingleton('adminhtml/session')->getNonCancelledOrders();
if (!$orderIds) {
$orderIds = array($order->getId());
} else {
$orderIds[] = $order->getId();
}
Mage::getSingleton('adminhtml/session')->setNonCancelledOrders($orderIds);
}
接下来,在以下事件的etc / config.xml文件中再添加一个观察者:&#34; controller_action_predispatch&#34;:
<controller_action_predispatch>
<observers>
<check_session_message>
<type>singleton</type>
<class>Muk_OrderCancel_Model_Observer</class>
<method>checkSessionMessage</method>
</check_session_message>
</observers>
</controller_action_predispatch>
然后在你的观察者方法中:
public function checkSessionMessage($observer)
{
//Check if we have admin order view or grid action
$request = Mage::app()->getRequest();
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($module == 'admin' && $controller == 'sales_order') {
if ($action == 'view' || $action == 'index') {
//Check if we have orderIds
$orderIds = Mage::getSingleton('adminhtml/session')->getNonCancelledOrders();
if ($orderIds && count($orderIds) > 0) {
//Unset them from the session
Mage::getSingleton('adminhtml/session')->unsNonCancelledOrders();
//Clear success message
Mage::getSingleton('adminhtml/session')->getMessages(true);
//Add error message
Mage::getSingleton('adminhtml/session')->addError('You are not allowed to cancel the order(s)');
}
}
}
}