Magento 1 – How to Disable Order Cancel Button in Admin Order View Page

magento-1

I want to disable the cancel order button from Magento admin order view page as well as from order grid page.

Though I know using the Roles we can disable this but I want to do this for some specific condition for a role.

So I am trying the code mentioned on the following link https://stackoverflow.com/questions/32788869/how-to-prevent-an-order-from-getting-cancelled-using-event-observer on stackoverflow.com
Though the code does not hide the cancel button it try to prevent the order being cancel. But the code is not working for me and order is still canceled.

1) Do we have any approach using event observer to disable the particular button on order view page as well as order grid.

2) Do I need to override the core/Mage/Adminhtml/Block/Sales/Order/View.php file?

Please suggest some solution for same.

order grid

enter image description here

Best Answer

For this case,Better idea to use Event/Observer.

In magento,on class Mage_Adminhtml_Block_Widget_Container there have a function removeButton($id).

By using this function and with help event adminhtml_widget_container_html_before,

You can remove cancel button from order view.

<adminhtml>
    <events>
        <adminhtml_widget_container_html_before>
            <observers>
                <your_module>
                    <class>your_module/observer</class>
                    <type>singleton</type>
                    <method>adminhtmlWidgetContainerHtmlBefore</method>
                </your_module>
            </observers>
        </adminhtml_widget_container_html_before>
    </events>
</adminhtml>

Observer:

public function adminhtmlWidgetContainerHtmlBefore($event)
{
    $block = $event->getBlock();

    if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {

        $block->removeButton('order_cancel');
    }
}

You see more details at https://stackoverflow.com/a/25565340/2940291

[Edit:]

remove cancel from order grid

use core_block_abstract_prepare_layout_before

<!-- another events may which remove button from grid -->

<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <remove_cancel_from_grid>
                    <type>singleton</type>
                    <class>your_model/observerr</class>
                    <method>removeButtoncancel</method>
                </remove_cancel_from_grid>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
</adminhtml>

observer.

public function removeButtoncancel($observer)
{   
    $block = $observer->getEvent()->getBlock();
    if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $block->removeItem('cancel_order');
    }
}

As observer is not works then try with block class rewrite Mage_Adminhtml_Block_Sales_Order_Grid.

Define rewrite in config.xml

  <adminhtml>
            <rewrite>
                <sales_order_grid>[ModuleNameSpace]_[ModuleName]_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtm

l>

Rewrite class

<?php
class [ModuleNameSpace]_[ModuleName]_Block_Adminhtml_Sales_Order_Grid extends
Mage_Adminhtml_Block_Sales_Order_Grid{


    protected function _prepareMassaction()
    {
        parent::_prepareMassaction();
         $this->getMassactionBlock()->removeItem('cancel_order');    
        return $this;
    }
}

Get full module:

Goto git hub

Related Topic