Site icon MageComp Blog

Magento 2: How to Display a Custom Amount in the Admin Order View Order Summary

Hello Magento Friends,

In today’s blog, we will learn how to display custom fee in summary on the admin order view page in Magento 2.

Magento 2 is a highly customizable platform that allows developers to modify almost every aspect of its functionality. One common requirement is to display custom fees or charges in the order summary on the Admin Order View page. This can be useful for adding special handling fees, additional service charges, or any other custom costs associated with an order.

In this blog post, we’ll walk through the steps to add a custom fee and display it in the summary section of the Admin Order View page in Magento 2.

Steps to Display a Custom Amount in the Admin Order View Order Summary in Magento 2:

Step 1: First, we need to create a “sales_order_view.xml” file inside our extension at the following path

app/code/Vendor/Extension/view/adminhtml/layout/sales_order_view.xml

Then add the code as follows

<?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>
        <referenceContainer name="order_totals">
            <block class="Vendor\Extension\Block\Adminhtml\Sales\Customfee" name="customfee"/>
        </referenceContainer>
    </body>
</page>

Step 2: After that, we need to create a “Customfee.php” file inside our extension at the following path

app/code/Vendor/Extension/Block/Adminhtml/Sales/Customfee.php

And include the code as given below

<?php
namespace Vendor\Extension\Block\Adminhtml\Sales;
 
use Magento\Sales\Model\Order;
 
class Customfee extends \Magento\Framework\View\Element\Template
{
    /**
     * @var Order
     */
    protected $_order;
    /**
     * @var \Magento\Framework\DataObject
     */
    protected $_source;
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }
    public function getSource()
    {
        return $this->_source;
    }
 
    public function displayFullSummary()
    {
        return true;
    }
    public function initTotals()
    {
        $parent = $this->getParentBlock();
        $this->_order = $parent->getOrder();
        $this->_source = $parent->getSource();
        $title = 'Custom Fee';
        $store = $this->getStore();
        if($this->_order->getFee()!=0){
            $customAmount = new \Magento\Framework\DataObject(
                    [
                        'code' => 'customfee',
                        'strong' => false,
                        'value' => $this->_order->getFee(),
                        'label' => __($title),
                    ]
                );
            $parent->addTotal($customAmount, 'customfee');
        }
        return $this;
    }
    /**
     * Get order store object
     *
     * @return \Magento\Store\Model\Store
     */
    public function getStore()
    {
        return $this->_order->getStore();
    }
    /**
     * @return Order
     */
    public function getOrder()
    {
        return $this->_order;
    }
}

Output:

Conclusion:

By following the steps outlined in this blog post, you can successfully add and display a custom fee in the Admin Order View Order Summary in Magento 2. This customization can help provide a better administrative experience and allow you to include additional charges directly within the order details.

Hire a Magento Developer to further customize the admin sales order view of your Magento 2 store for the smooth functioning of the admin panel.

Happy Coding!

Exit mobile version