Cómo agregar un nuevo botón a la vista de pedido en el panel de administración de Magento?


¿Cómo agregar un botón personalizado a la página de vista de orden cerca de "Atrás" y "Editar"?

Author: silex, 2011-07-10

4 answers

En lugar de hacks de núcleo o reescrituras, simplemente use un observador para agregar el botón al orden:

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

Entonces simplemente verifique en el observador si el tipo del bloque coincide con la vista de orden:

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

La función "getURL" del bloque agregará automáticamente el id de orden actual a la llamada del controlador.

 38
Author: Matthias Kleine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-29 09:22:12

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');
    }
}
 24
Author: silex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-08 08:57:27

En referencia a los comentarios anteriores sobre el constructor parent::__, aquí está lo que funcionó para mí:

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();

    }
}
 2
Author: Tom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-25 17:12:08

Si quieres hacerlo rápido y sucio (es decir, editar archivos principales), abre app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php y agrega algo como:

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

Puedes colocar eso antes de este bloque:

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

Su desafío, si elige aceptar, es crear un archivo en local que sea una sobrecarga del archivo principal, y publicarlo aquí!

 -2
Author: ʍǝɥʇɐɯ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-10 18:56:46