1

Today I am trying to overwrite the \Magento\Sales\Block\Adminhtml\Order\View file, but it's not working. The same coding working on core files.

My Code is

<?php

namespace SR\StackExchange\Block\Adminhtml\Order;

class View extends \Magento\Sales\Block\Adminhtml\Order\View
{

    /**
     * Get URL for back (reset) button
     *
     * @return string
     */
    public function getCustomOrderUrl()
    {
        $params = [
            'id' => $this->getOrderId(),
            'store' => 0
        ];

        return $this->getUrl('stackexchange/stackexchange/order');
    }

    /**
     * Constructor
     *
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _construct()
    {

        $this->buttonList->add(
            'custom_button',
            [
                'label' => __('Custom Button'),
                'class' => 'action-secondary',
                'on_click' => sprintf("location.href = '%s';", $this->getCustomOrderUrl()),
            ]
        );

        parent::_construct();
    }

}

etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">   
    <type name="Magento\Sales\Block\Adminhtml\Order\View">
        <plugin name="custom_button" type="SR\StackExchange\Block\Adminhtml\Order\View" sortOrder="1"/>
    </type>
</config>
2

I think you misunderstanding plugins and preferences.

If you want to override an entire class you need to use preferences:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">   
    <preference for="Magento\Sales\Block\Adminhtml\Order\View"
                type="SR\StackExchange\Block\Adminhtml\Order\View" />
</config>

You can get a good read about the differences between plugins and preferences here: Magento2: what is the basic difference between plugin and preference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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