Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento 2: add button to salse order or shipping page

04 Jan 2022 » magento2, php

add plugin

app/code/Firstflight/Firstship/etc/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\Shipping\Block\Adminhtml\View">
        <plugin disabled="false" name="Magento_Shipping_Block_Adminhtml_View" 
            type="Firstflight\Firstship\Plugin\Magento\Shipping\Block\Adminhtml\View"/>
    </type>
</config>
    

plugin

app/code/Firstflight/Firstship/Plugin/Magento/Shipping/Block/Adminhtml/View.php

<?php
namespace Firstflight\Firstship\Plugin\Magento\Shipping\Block\Adminhtml;

use Magento\Shipping\Block\Adminhtml\View as Subject;

/**
 * View class for add button and template of generate awb number
 * and print pdf shipment
 */
class View
{

    /**
     * before plugin for setlayout
     *
     * @param Subject $subject
     * @param mix $layout
     * @return mix
     */
    public function beforeSetLayout(
        Subject $subject,
        $layout
    ) {
        $subject->addButton('generate_awb_number', [
            'label' => __('Generate AWB number'),
            'onclick' => 'openFormModel();',
            'class' => 'action-default action-generate-awb-number',
        ]);
        $subject->addButton('print_packaging_slip', [
            'label' => __('Print packaging slip'),
            'onclick' => 'openPrintModel()',
            'class' => 'action-default action-print-packaging-slip',
        ]);
        return [$layout];
    }

    /**
     * after plugin for tohtml method
     *
     * @param Subject $subject
     * @param String $result
     * @return String
     */
    public function afterToHtml(Subject $subject, $result)
    {
        if ($subject->getNameInLayout() == 'sales_shipment_view') {
            $customBlockHtml = $subject->getLayout()->createBlock(
                \Firstflight\Firstship\Block\Adminhtml\AwbTemplate::class,
                $subject->getNameInLayout().'_modal_box'
            )
            ->setShipping($subject->getShipment())
            ->setFormUrl(
                $subject->getUrl('firstflight/action/generateawbnumber', [
                    'shipment_id' => $subject->getShipment()->getId(),
                    'order_id' => $subject->getShipment()->getOrderId(),
                ])
            )->setTemplate('Firstflight_Firstship::order/modalbox.phtml')->toHtml();
            return $result.$customBlockHtml;
        }
        return $result;
    }
}

add template

<div id="print-bill-model">
    <table class="data-table admin__control-table">
        <?php foreach ($tracks as $key => $track) {?>
            <tr>
                <th>
                    <?= $escaper->escapeHtml($track->getData('title'))?>
                </th>
                <th>
                    <?= $escaper->escapeHtml($track->getData('track_number'))?>
                </th>
                <td>
                    <button onclick="window.open(
                        '<?= $escaper->escapeUrl($block->getUrl('firstflight/action/printpackagingslip', [
                            'tracking_id' => $track->getData('entity_id')
                        ])) ?>','_blank'
                    )">
                        <?= $escaper->escapeHtml(__("Print this shiment"))?>
                    </button>
                </td>
            </tr>
        <?php } ?>
    </table>
</div>

<script>
    require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function (
            $,
            modal
        ) {
            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: '<?= $escaper->escapeHtml(__("Airway Bill Data"))?>',
                modalClass: 'custom-modal',
                buttons: [
                    {
                        text: $.mage.__('Confirm'),
                        class: '',
                        click: function () {
                            $('#generate_awb_number_form').append($('<input>', {
                                'name': 'form_key',
                                'value': window.FORM_KEY,
                                'type': 'hidden'
                            }));
                            $('#generate_awb_number_form').submit();
                        }
                    },
                ]
            };
            var popup = modal(options, $('#popup-modal'));

            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: '<?= $escaper->escapeHtml(__("Print airway bill"))?>',
                modalClass: 'custom-modal',
                buttons: [
                    {
                        text: $.mage.__('Close'),
                        class: '',
                        click: function () {
                            $("#print-bill-model").modal('closeModal');
                        }
                    },
                ]
            };
            var printPopup = modal(options, $('#print-bill-model'));
        }
 
    );
    function openFormModel() {
        require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function (
            $,
            modal
        ) {
            $("#popup-modal").modal('openModal');
        });
    }
    function openPrintModel() {
        require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function (
            $,
            modal
        ) {
            $("#print-bill-model").modal('openModal');
        });
    }
</script>

Thanks.