2

Using magento 2.2, admin, sale->order. I want to hide button Invoice when status Pending.

enter image description here

2 Answers 2

2

Try this :

vendor\magento\module-sales\view\adminhtml\layout\sales_order_view.xml override file in your module

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="sales_order_transactions_grid_block"/>
    <update handle="sales_order_item_price"/>
    <body>
         <referenceContainer name="content">
            <block class="Vendor\Module\Block\Adminhtml\Order\View" name="sales_order_edit"/>
        </referenceContainer>
    </body>
</page>

create a file View.php

<?php

namespace Vendor\Module\Block\Adminhtml\Order;

class View extends \Magento\Sales\Block\Adminhtml\Order\View
{
    protected function _construct()
    {
        $this->_objectId = 'order_id';
        $this->_controller = 'adminhtml_order';
        $this->_mode = 'view';

        parent::_construct();


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

        if (!$order) {
            return;
        }

        if($order->getStatus() != 'pending'){

            if ($this->_isAllowedAction('Magento_Sales::invoice') && $order->canInvoice()) {
                $_label = $order->getForcedShipmentWithInvoice() ? __('Invoice and Ship') : __('Invoice');
                $this->addButton(
                    'order_invoice',
                    [
                        'label' => $_label,
                        'onclick' => 'setLocation(\'' . $this->getInvoiceUrl() . '\')',
                        'class' => 'invoice'
                    ]
                );
            }
        }        
    }
}
1

override \Magento\Sales\Block\Adminhtml\Order\View.php Class and add condition where invoice button created as below:

if($order->getStatus() != 'pending')
        {
           if ($this->_isAllowedAction('Magento_Sales::invoice') && $order->canInvoice()) {
                $_label = $order->getForcedShipmentWithInvoice() ? __('Invoice and Ship') : __('Invoice');
                $this->addButton(
                    'order_invoice',
                    [
                        'label' => $_label,
                        'onclick' => 'setLocation(\'' . $this->getInvoiceUrl() . '\')',
                        'class' => 'invoice'
                    ]
                );
            } 
        }

Check result Screenshot: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.