Magento2 – Custom Admin Button to Sales Order View Without Plugin
February 5, 2022
Add Custom Admin Button to Sales Order View Without Plugin Magento2
We have already posted an article on adding a Custom Admin Button to Sales Order View by using a plugin
In this, We will create the Custom Admin Button without using the plugin.
Let’s have a look into code
Step 1: Create a sales_order_view.xml file in <vendor>/<module>/view/adminhtml/layout/sales_order_view.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="sales_order_edit"> <block class="<vendor>\<module>\Block\Adminhtml\Order\View\Buttons" name="custom_buttons" /> </referenceBlock> </body> </page> |
Step 2: Create a file in <vendor>/<module>/Block/Adminhtml/Order/View/Buttons.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
namespace <vendor>\<module>\Block\Adminhtml\Order\View; class Buttons extends \Magento\Sales\Block\Adminhtml\Order\View { protected function _construct() { parent::_construct(); if(!$this->getOrderId()) { return $this; } $buttonUrl = $this->_urlBuilder->getUrl( 'adminhtml/custombutton/new', ['order_id' => $this->getOrderId()] ); $this->addButton( 'create_custom_button', ['label' => __('Custom Button'), 'onclick' => 'setLocation(\'' . $buttonUrl . '\')'] ); return $this; } } |
You may Like this :
- Learn How To Create A Module Magento 2!
- Product Types in Magento 2
- Difference Between Magento 2 Modes
Then, Run the cache flush command:
1 |
php bin/magento c:f |
Now, You can check the order view.
Check out our Latest Magento Posts
—