Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

How to Display Custom Price fee in Sales Order View Page

You can learn how to add a custom amount in order total from here:

http://webkul.com/blog/add-custom-pricefee-order-total-magento2/

Now, if you want to display the custom price fee on the sales order view page, then you can follow the following process:

firstly, you need to create a sales_order_view.xml file in the folder:

app/code/Webkul/Test/view/frontend/layout

<?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="Webkul\Test\Block\Sales\Order\Test" name="customfee"/>
        </referenceContainer>
    </body>
</page>

Now, you need to define a block file in which you need to define the init_totals() function, which adds a custom fee amount to your order view.

<?php
namespace Webkul\Test\Block\Sales\Order;

use Magento\Sales\Model\Order;

class Test 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->getCustomfee()!=0){
            $customAmount = new \Magento\Framework\DataObject(
                    [
                        'code' => 'customfee',
                        'strong' => false,
                        'value' => $this->_order->getCustomfee(),
                        '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();
    }
}

Here, the init_totals() function adds the custom fee to your order by using the addTotal() method.

$customAmount is an object of dataObject that stores the label, code, and value for a custom fee.

‘customfee’ is the custom amount field name.

After this, your custom fee amount is visible on the customer’s order view page.

I hope this blog will help you with How to Display Custom Price fees on the Sales Order View Page. You may also check our wide range of best Magento 2 Extensions.

Please reach out to our team via a support ticket if you have any queries.

Try this and if you have any queries then just comment below 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • tallenmusclegeeks
    • Bulbul agarwal
      • tallenmusclegeeks
  • Display custom price fee on checkout cart and summary total in magento2
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home