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>
Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Jul 7, 2016 at 11:33

1 Answer 1

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?

answered Jul 7, 2016 at 11:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.