How to add new button to order view in Magento admin panel?

后端 未结 4 1954
情书的邮戳
情书的邮戳 2021-02-01 05:02

How to add custom button to order view page near \"Back\" and \"Edit\"?

相关标签:
4条回答
  • 2021-02-01 05:14

    If you want to do it quick-and-dirty (i.e. editing core files), open app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php and add something like:

        $this->_addButton('order_reorder', array(
            'label'     => Mage::helper('sales')->__('Print Labels'),
            'onclick'   => 'window.open(\'/printouts/' . $this->getOrder()->getRealOrderId() . '.pdf\')',
        ));
    

    You can place that before this block:

        if ($this->_isAllowedAction('emails') && !$order->isCanceled()) {
            $message = Mage::helper('sales')->__('Are you sure you want to send order email to customer?');
            $this->addButton('send_notification', array(
                'label'     => Mage::helper('sales')->__('Send Email'),
                'onclick'   => "confirmSetLocation('{$message}', '{$this->getEmailUrl()}')",
            ));
        }
    

    Your challenge, should you choose to accept is to create a file in local that is an over-ride of the core file, and to post it here!

    0 讨论(0)
  • 2021-02-01 05:19

    config.xml:

    <global>
        <blocks>
             <adminhtml>
                <rewrite>
                    <sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
                </rewrite>
            </adminhtml>
        </blocks>
     </global>
    

    Namespace/Module/Block/Adminhtml/Sales/Order/View.php:

    class Namespace_Module_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
        public function  __construct() {
    
            parent::__construct();
    
            $this->_addButton('button_id', array(
                'label'     => Mage::helper('xxx')->__('Some action'),
                'onclick'   => 'jsfunction(this.id)',
                'class'     => 'go'
            ), 0, 100, 'header', 'header');
        }
    }
    
    0 讨论(0)
  • 2021-02-01 05:26

    In reference to the comments above about the parent::__constructor, here is what worked for me:

    class Name_Module_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
    
        public function  __construct() {
            $this->_addButton('testbutton', array(
                'label'     => Mage::helper('Sales')->__('Toms Button'),
                'onclick'   => 'jsfunction(this.id)',
                'class'     => 'go'
            ), 0, 100, 'header', 'header');
    
            parent::__construct();
    
        }
    }
    
    0 讨论(0)
  • 2021-02-01 05:37

    Instead of core hacks or rewrites, just use an observer to add the button to the order:

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

    Then just check in the observer if the type of the block matches the order view:

    public function adminhtmlWidgetContainerHtmlBefore($event)
    {
        $block = $event->getBlock();
    
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
            $message = Mage::helper('your_module')->__('Are you sure you want to do this?');
            $block->addButton('do_something_crazy', array(
                'label'     => Mage::helper('your_module')->__('Export Order'),
                'onclick'   => "confirmSetLocation('{$message}', '{$block->getUrl('*/yourmodule/crazy')}')",
                'class'     => 'go'
            ));
        }
    }
    

    The "getUrl" function of the block will automatically append the current order id to the controller call.

    0 讨论(0)
提交回复
热议问题