Reading list Switch to dark mode

    Custom Modify and Create custom order totals variations in Magento 2

    Updated 27 September 2021

    Today, We are going to check how can we mange Order of Custom Fee in Magento 2. You can follow it for any Custom Order Total.

    Firstly, Please check the blog to know How to display custom price fee in sales order view page

    After following the above blog the result will be:

    Default Order

    The above mentioned i the default order. If you have added any custom discount. It will be appear at the end and just before the Grand Total.

    Now, If we want the our Custom Fee at the very top (Before all Order Total):

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    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, 'first');
            }
            return $this;
        }

    You can check in above snippet code we have mentioned the order first with passing second parameter in addTotal method. It will display our Custom Fee at the top. See the result below:

    first

    Now, We check if we want to display our Custom Fee at the very bottom (after the Grant Total OR Base Grand Total).

    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, 'last');
            }
            return $this;
        }

    You can check in above snippet code we have mentioned the order last with passing second parameter in addTotal method. It will display our Custom Fee at the bottom. See the result below:

    Last

    In the above result you can check with the help of “last”. Now, our custom discount is visible at the bottom and after the Grand Total.

    Now, We will check if we want to display after any particular Order Total. In this example we are going to display our Order Total just after the shipping amount.

    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, 'shipping');
            }
            return $this;
        }

    You can check in above snippet code we have mentioned the order by passing second parameter the particular total code shipping in addTotal method. It will display our Custom Fee just after the Shipping amount. See the result below:

    After any total

    Now, at last we are going to check. How can we add display our Custom Fee just before any Order Total. In this example we are going to display our Custom Fee just before the Shipping.

    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->addTotalBefore($customAmount, 'shipping');
            }
            return $this;
        }

    You can check in above snippet code we have mentioned the order by passing second parameter the particular total code shipping in addTotalBefore method. It will display our Custom Fee just before the Shipping amount. See the result below:

    Before Order Total

    You can check in above screenshot. Our Custom Fee just before the Shipping & Handling. We have used addTotal by addTotalBefore to display our Custom Fee just before any Order Total.

    That’s It. Hope it will helped you. Please let us know by commenting if any query.

    Happy Coding 🙂

    . . .

    Leave a Comment

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


    2 comments

  • Sandeep Kumar
  • Back to Top

    Message Sent!

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

    Back to Home