We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    To add a custom button on the admin order view page, you need to create a before plugin for the PushButtons() method from the MagentoBackendBlockWidgetButtonToolbar class.

    Create di.xml file under the adminhtml scope,
    Path: app/code/Magemonkeys/OrderViewButton/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">
        <!-- Add new Button on sales order view toolbar -->
        <type name="MagentoBackendBlockWidgetButtonToolbar">
            <plugin name="add_sales_button_toolbar" type="MagemonkeysOrderViewButtonPluginBlockWidgetButtonToolbarPlugin" />
        </type>
    </config>

    create ToolbarPlugin.php
    Path: app/code/Magemonkeys/OrderViewButton/Plugin/Block/Widget/Button/ToolbarPlugin.php

    <?php declare(strict_types=1);
    
    namespace MagemonkeysOrderViewButtonPluginBlockWidgetButton;
    
    use MagentoSalesBlockAdminhtmlOrderCreate;
    use MagentoFrameworkViewElementAbstractBlock;
    use MagentoBackendBlockWidgetButtonButtonList;
    use MagentoBackendBlockWidgetButtonToolbar as ToolbarContext;
    
    class ToolbarPlugin
    {
        /**
         * @param ToolbarContext $toolbar
         * @param AbstractBlock $context
         * @param ButtonList $buttonList
         * @return array
         */
        public function beforePushButtons(
            ToolbarContext $toolbar,
            AbstractBlock $context,
            ButtonList $buttonList
        ): array {
            $order = false;
            $nameInLayout = $context->getNameInLayout();
            if ('sales_order_edit' == $nameInLayout) {
                $order = $context->getOrder();
            }
    
            if ($order) {
            	$url = "YOUR_URL"; // add your full url
    	        $buttonList->add(
    	            'my_button',
    	            [
    	                'label' => __('My Button'),
    	            	'on_click' => sprintf("location.href = '%s';", $url),
    	                'class' => 'primary',
    	                'id' => 'my_button'
    	            ]
    	        );
            }
    
            return [$context, $buttonList];        
        }
    }<code>

    Output :

    field_5bfb909c5ccae

      Get a Free Quote