How To

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!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

12 hours ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

3 days ago

Hyvä Theme FAQs

Hyvä is gradually gaining popularity in this current market, and even 3.5% of Magento websites…

4 days ago

What is Curbside Pickup?

In today’s fast-paced society, where convenience and safety are paramount, curbside pickup has emerged as…

4 days ago

What is a Planogram?

Have you ever observed how complementary and similar items are often displayed together in brick-and-mortar…

4 days ago

Hyvä Checkout – A Real Game Changer

You may be familiar with Hyvä, the frontend theme for Magento 2, which has been…

4 days ago