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.
Contents
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
1 2 3 4 5 6 7 8 |
<?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
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?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!