Skip to content

how to display custom amount in order summary in admin Magento 2?

I have added a custom fee in the order and displayed a custom fee in the checkout order summary.

Similarly, the Custom fee displayed in the order emails in order total summary.

Now I am trying to show the custom fee in summary on the admin order view page.

I have used the below code to display the custom fee on the admin order view page:

1. sales_order_view.xml

<referenceContainer name="order_totals">
        <block class="VendorModuleBlockAdminhtmlSalesTotals" name="fee"/>
    </referenceContainer>

2. Totals.php

<?php

    
namespace VendorModuleBlockAdminhtmlSales;

class Totals extends MagentoFrameworkViewElementTemplate
{

    /**
     * @var Sivajik34CustomFeeHelperData
     */
    protected $_dataHelper;

    /**
     * @var MagentoDirectoryModelCurrency
     */
    protected $_currency;

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        VendorModuleHelperData $dataHelper,
        MagentoDirectoryModelCurrency $currency,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_dataHelper = $dataHelper;
        $this->_currency = $currency;
    }

    /**
     * Retrieve current order model instance
     *
     * @return MagentoSalesModelOrder
     */
    public function getOrder()
    {
        return $this->getParentBlock()->getOrder();
    }

    /**
     * @return mixed
     */
    public function getSource()
    {
        return $this->getParentBlock()->getSource();
    }

    /**
     * @return string
     */
    public function getCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }

    /**
     *
     *
     * @return $this
     */
    public function initTotals()
    {
        $this->getParentBlock();
        $this->getOrder();
        $this->getSource();

        $fee = (int) $this->getSource()->getFee();

        if (!$fee) {
            return $this;
        }
        $total = new MagentoFrameworkDataObject([
            'code' => 'fee',
            'value' => $this->getSource()->getFee(),
            'label' => $this->_dataHelper->getFeeLabel(),
        ]);
        $this->getParentBlock()->addTotalBefore($total, 'grand_total');

        return $this;
    }
}

But the custom fee is not showing on the admin order view page. can anyone suggest where I am wrong?

I followed this tutorial Custom Fee