0

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="Vendor\Module\Block\Adminhtml\Sales\Totals" name="fee"/>
    </referenceContainer>

2. Totals.php

<?php

    
namespace Vendor\Module\Block\Adminhtml\Sales;

class Totals extends \Magento\Framework\View\Element\Template
{

    /**
     * @var \Sivajik34\CustomFee\Helper\Data
     */
    protected $_dataHelper;

    /**
     * @var \Magento\Directory\Model\Currency
     */
    protected $_currency;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Vendor\Module\Helper\Data $dataHelper,
        \Magento\Directory\Model\Currency $currency,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_dataHelper = $dataHelper;
        $this->_currency = $currency;
    }

    /**
     * Retrieve current order model instance
     *
     * @return \Magento\Sales\Model\Order
     */
    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 \Magento\Framework\DataObject([
            '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

4
  • Have you got custom fee by $this->getSource()->getFee()? Have you checked?
    – ZealousWeb
    May 31, 2021 at 12:07
  • I think initTotals() is not working...I tried to add log in that function but not working or even use exit but there is no change May 31, 2021 at 12:59
  • In which path you have added file sales_order_view.xml?
    – ZealousWeb
    May 31, 2021 at 13:03
  • Vendor\Module\view\adminhtml\layout\sales_order_view.xml May 31, 2021 at 13:06

2 Answers 2

0

Please add like below in Totals.php file

<?php
namespace Vendor\Module\Block\Adminhtml\Sales;
 
use Magento\Sales\Model\Order;
 
class Totals 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;
    }
    /**
     * @return array
     */
    public function getLabelProperties()
    {
        return $this->getParentBlock()->getLabelProperties();
    }
    /**
     * @return array
     */
    public function getValueProperties()
    {
        return $this->getParentBlock()->getValueProperties();
    }
}

Please check and let me know on this.

7
  • Not working, still the same issue May 31, 2021 at 12:52
  • in which magento version you are working?
    – ZealousWeb
    May 31, 2021 at 12:53
  • Magento version 2.2.4 May 31, 2021 at 12:57
  • I have updated my answer. Please make sure that you are getting your fee value in $this->_order->getFee(). Else please add static fee value 10 and then please check
    – ZealousWeb
    May 31, 2021 at 13:02
  • I tried even static value but not working and order total section is blank: nimb.ws/wZTubp May 31, 2021 at 13:15
0

You have an error in your first step. You need to change <referenceContainer name="order_totals"> to

<referenceBlock name="order_totals">

reference vendor/magento/module-sales/view/adminhtml/layout/sales_order_view.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.