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:
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):
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:
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:
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:
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:
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 🙂
2 comments
Greetings
Using the code in this blog -> https://webkul.com/blog/add-custom-pricefee-order-total-magento2/
One can display the custom fee/ Tax in the order section at admin order create page.
https://prnt.sc/1MnK0AubVjB6
If you still face any query. Please share at ([email protected])
Thanks and regards
Team Webkul