Magento Admin – Fix Rewrite Block Not Working Properly

adminhtmlblocksoverrides

all
Today , i am trying to rewrite admin sales order view block(class is Mage_Adminhtml_Block_Sales_Order_View) it is working.But when i am trying to add extra button in view, then all the button has been varnish

Here My code:

1)app/code/local/Bh/Orderconfirmation/etc/config.xml

<?xml version="1.0" ?>
<config>
    <modules>
        <Bh_Orderconfirmation>
        <version>1.0.0</version>
        </Bh_Orderconfirmation>
    </modules>
    <!-- start of model define -->
    <global>
        <models>
            <orderconfirmation>
                <class>Bh_Orderconfirmation_Model</class>   
            </orderconfirmation>
        </models>
    <!-- start of block -->
        <blocks>
            <orderconfirmation>
                <class>Bh_Orderconfirmation_Block</class>   
            </orderconfirmation>
            <adminhtml>
                <rewrite>
                <sales_order_view>Bh_Orderconfirmation_Block_Adminhtml_Sales_Order_View</sales_order_view>      
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
    <!-- Start of Override admincontroller -->
    <admin>
        <routers>
            <adminhtml>
                <args>
                <modules>
                <Order_Controller_myOveridder before="Mage_Adminhtml">Bh_Orderconfirmation_Adminhtml</Order_Controller_myOveridder>
                </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>    
    <!--end of  admin controller Override -->
</config>

2)app/code/local/Bh/Orderconfirmation/Block/Adminhtml/Sales/Order/View.php

<?php
class Bh_Orderconfirmation_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View
{

    public function __construct()
    {

     parent::__construct();

    /* end of confirmation */
    }

    public function getIsConfirmedUrl(){
            return $this->getUrl('*/*/isconfirmed');
    }


}

3)app/code/local/Bh/Orderconfirmation/controllers/Adminhtml/Sales/OrderController.php

<?php
require_once  Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'Sales'.DS.'OrderController.php';

class Bh_Orderconfirmation_Adminhtml_Sales_Ordercontroller extends Mage_Adminhtml_Sales_OrderController{
    public function viewAction(){
        parent::viewAction();
    }

   public function isconfirmedAction()
    {
        if ($order = $this->_initOrder()) {
            try {

                 $order->setData('is_confirmed',true);
        $order->getResource()->saveAttribute($order,'is_confirmed');

                $this->_getSession()->addSuccess(
                    $this->__('The order has been put Confirmed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order was not put on confirmed.'));
            }
            $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
        }
    }
}

Till now is working and show all magento default all order button.
enter image description here

But when i added new button beside send mail Button(Confirm) in view.php then all button is vanish

Add only below on rewrite class of view.php

  public function __construct()
    {
        $this->_objectId    = 'order_id';
        $this->_controller  = 'sales_order';
        $this->_mode        = 'view';

        parent::__construct();

        $this->_removeButton('delete');
        $this->_removeButton('reset');
        $this->_removeButton('save');
        $this->setId('sales_order_view');
        $order = $this->getOrder();

        if ($this->_isAllowedAction('edit') && $order->canEdit()) {
            $onclickJs = 'deleteConfirm(\''
                . Mage::helper('sales')->__('Are you sure? This order will be canceled and a new one will be created instead')
                . '\', \'' . $this->getEditUrl() . '\');';
            $this->_addButton('order_edit', array(
                'label'    => Mage::helper('sales')->__('Edit'),
                'onclick'  => $onclickJs,
            ));
            // see if order has non-editable products as items
            $nonEditableTypes = array_keys($this->getOrder()->getResource()->aggregateProductsByTypes(
                $order->getId(),
                array_keys(Mage::getConfig()
                    ->getNode('adminhtml/sales/order/create/available_product_types')
                    ->asArray()
                ),
                false
            ));
            if ($nonEditableTypes) {
                $this->_updateButton('order_edit', 'onclick',
                    'if (!confirm(\'' .
                    Mage::helper('sales')->__('This order contains (%s) items and therefore cannot be edited through the admin interface at this time, if you wish to continue editing the (%s) items will be removed, the order will be canceled and a new order will be placed.', implode(', ', $nonEditableTypes), implode(', ', $nonEditableTypes)) . '\')) return false;' . $onclickJs
                );
            }
        }

        if ($this->_isAllowedAction('cancel') && $order->canCancel()) {
            $message = Mage::helper('sales')->__('Are you sure you want to cancel this order?');
            $this->_addButton('order_cancel', array(
                'label'     => Mage::helper('sales')->__('Cancel'),
                'onclick'   => 'deleteConfirm(\''.$message.'\', \'' . $this->getCancelUrl() . '\')',
            ));
        }

        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()}')",
            ));
        }  
         if ($this->_isAllowedAction('isconfirmed') && !$order->isCanceled()) {
            $this->_addButton('isconfirmed', array(
                'label'     => Mage::helper('sales')->__('Confirm'),
                'onclick'   => 'setLocation(\'' . $this->getIsConfirmedUrl() . '\')',
            ));
        }

        if ($this->_isAllowedAction('creditmemo') && $order->canCreditmemo()) {
            $message = Mage::helper('sales')->__('This will create an offline refund. To create an online refund, open an invoice and create credit memo for it. Do you wish to proceed?');
            $onClick = "setLocation('{$this->getCreditmemoUrl()}')";
            if ($order->getPayment()->getMethodInstance()->isGateway()) {
                $onClick = "confirmSetLocation('{$message}', '{$this->getCreditmemoUrl()}')";
            }
            $this->_addButton('order_creditmemo', array(
                'label'     => Mage::helper('sales')->__('Credit Memo'),
                'onclick'   => $onClick,
                'class'     => 'go'
            ));
        }

        // invoice action intentionally
        if ($this->_isAllowedAction('invoice') && $order->canVoidPayment()) {
            $message = Mage::helper('sales')->__('Are you sure you want to void the payment?');
            $this->addButton('void_payment', array(
                'label'     => Mage::helper('sales')->__('Void'),
                'onclick'   => "confirmSetLocation('{$message}', '{$this->getVoidPaymentUrl()}')",
            ));
        }

        if ($this->_isAllowedAction('hold') && $order->canHold()) {
            $this->_addButton('order_hold', array(
                'label'     => Mage::helper('sales')->__('Hold'),
                'onclick'   => 'setLocation(\'' . $this->getHoldUrl() . '\')',
            ));
        }

        if ($this->_isAllowedAction('unhold') && $order->canUnhold()) {
            $this->_addButton('order_unhold', array(
                'label'     => Mage::helper('sales')->__('Unhold'),
                'onclick'   => 'setLocation(\'' . $this->getUnholdUrl() . '\')',
            ));
        }

        if ($this->_isAllowedAction('review_payment')) {
            if ($order->canReviewPayment()) {
                $message = Mage::helper('sales')->__('Are you sure you want to accept this payment?');
                $this->_addButton('accept_payment', array(
                    'label'     => Mage::helper('sales')->__('Accept Payment'),
                    'onclick'   => "confirmSetLocation('{$message}', '{$this->getReviewPaymentUrl('accept')}')",
                ));
                $message = Mage::helper('sales')->__('Are you sure you want to deny this payment?');
                $this->_addButton('deny_payment', array(
                    'label'     => Mage::helper('sales')->__('Deny Payment'),
                    'onclick'   => "confirmSetLocation('{$message}', '{$this->getReviewPaymentUrl('deny')}')",
                ));
            }
            if ($order->canFetchPaymentReviewUpdate()) {
                $this->_addButton('get_review_payment_update', array(
                    'label'     => Mage::helper('sales')->__('Get Payment Update'),
                    'onclick'   => 'setLocation(\'' . $this->getReviewPaymentUrl('update') . '\')',
                ));
            }
        }

        if ($this->_isAllowedAction('invoice') && $order->canInvoice()) {
            $_label = $order->getForcedDoShipmentWithInvoice() ?
                Mage::helper('sales')->__('Invoice and Ship') :
                Mage::helper('sales')->__('Invoice');
            $this->_addButton('order_invoice', array(
                'label'     => $_label,
                'onclick'   => 'setLocation(\'' . $this->getInvoiceUrl() . '\')',
                'class'     => 'go'
            ));
        }

        if ($this->_isAllowedAction('ship') && $order->canShip()
            && !$order->getForcedDoShipmentWithInvoice()) {
            $this->_addButton('order_ship', array(
                'label'     => Mage::helper('sales')->__('Ship'),
                'onclick'   => 'setLocation(\'' . $this->getShipUrl() . '\')',
                'class'     => 'go'
            ));
        }

        if ($this->_isAllowedAction('reorder')
            && $this->helper('sales/reorder')->isAllowed($order->getStore())
            && $order->canReorderIgnoreSalable()
        ) {
            $this->_addButton('order_reorder', array(
                'label'     => Mage::helper('sales')->__('Reorder'),
                'onclick'   => 'setLocation(\'' . $this->getReorderUrl() . '\')',
                'class'     => 'go'
            ));
        }
    }

Result only Show Two Buttons and other missing:
enter image description here

I thought i may be __construct() function.Can any one help me

Best Answer

Take a look at Magento CE 1.7.0.2 - Adding Next Previous in ADMIN sales order view product try calling the parent::__construct(); last.

class MageIgniter_NextBackButton_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
   ....

     public function  __construct() {
     if($url = $this->getPreviousUrl()){
       $this->_addButton('previous_button', array(
           'label'     => Mage::helper('sales')->__('Previous'),
           'onclick'   => 'setLocation(\'' . $url . '\')',
           'class'     => 'go'
       ));
     }

     parent::__construct();
}

Also another way to add a button without rewriting is to use an observer

In config.xml

<events>
    <adminhtml_block_html_before>
        <observers>
            <orderconfirmation>
                <class>orderconfirmation/observer</class>
                <method>orderPageButton</method>
                <type>singleton</type>
            </orderconfirmation>
        </observers>
    </adminhtml_block_html_before>
</events>

In observer.php

public function orderPageButton(Varien_Event_Observer $observer) {
    $block = $observer->getEvent()->getData('block');

    if ($block->getId() == 'sales_order_view' && $block->getRequest()->getControllerName() == 'sales_order') {

            $block->addButton('guest_to_customer', array(
                'label' => Mage::helper('sales')->__('Convert To Customer'),
                'onclick' => "confirmSetLocation('{$message}', '{$url}')",
            ));

    }
}
Related Topic