Mukesh
Mukesh

Reputation: 7778

How to prevent an order from being cancelled using event observer?

I am using the following code to prevent the order from being cancelled from the Magento admin panel.

<?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>

In the observer I am using the following code:

<?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);            
        }       
    }
}

But even after enabling this module, the order is getting cancelled.

How can I prevent the order from being cancelled?

Upvotes: 0

Views: 1921

Answers (1)

zokibtmkd
zokibtmkd

Reputation: 2183

In the "Mage_Adminhtml_Sales_OrderController" "cancelAction" it goes:

$order->cancel()
       ->save();

which means the order is first cancelled then your observer fires. Although I found this event:

Mage::dispatchEvent('sales_order_payment_cancel', array('payment' => $this));

in "Mage_Sales_Model_Order_Payment" which fires before: "Mage_Sales_Model_Order" : "registerCancellation" method.

In your observer method which fires on this event you can do this:

if ($adminRoleName) {
$payment = $observer-&gt;getEvent()-&gt;getPayment();
$order = $payment-&gt;getOrder();
$order-&gt;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')-&gt;getNonCancelledOrders();
if (!$orderIds) {
    $orderIds = array($order-&gt;getId());
} else {
    $orderIds[] = $order-&gt;getId();
}
Mage::getSingleton('adminhtml/session')-&gt;setNonCancelledOrders($orderIds);
}

Next, add one more observer in your etc/config.xml file on the following event: "controller_action_predispatch":

<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>

Then in your observer method:

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)');
            }
        }
    }
}

Upvotes: 2

Related Questions