Read More
Read More
Menu Close

    How to add custom price/fee in order total magento2

    If you want to add custom price/custom fee in your total, then you can use following approach.

    create sales.xml in you module etc folder

    <?xml version="1.0"?>
    <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
        <section name="quote">
            <group name="totals">
                <item name="customfee" instance="Webkul\Test\Model\Total\Customfee" sort_order="150"/>

    Now, define app\code\Webkul\Test\Model\Total\Customfee.php, where you update your total according to your custom fee.

    class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
         * Collect grand total address amount
         * @param \Magento\Quote\Model\Quote $quote
         * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
         * @param \Magento\Quote\Model\Quote\Address\Total $total
         * @return $this
        protected $quoteValidator = null; 
        public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
            $this->quoteValidator = $quoteValidator;
      public function collect(
            \Magento\Quote\Model\Quote $quote,
            \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
            \Magento\Quote\Model\Quote\Address\Total $total
        ) {
            parent::collect($quote, $shippingAssignment, $total);
            $exist_amount = 0; //$quote->getCustomfee(); 
            $customfee = 100; //enter amount which you want to set
            $balance = $customfee - $exist_amount;//final amount
            $total->setTotalAmount('customfee', $balance);
            $total->setBaseTotalAmount('customfee', $balance);
            $total->setGrandTotal($total->getGrandTotal() + $balance);
            $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);
            return $this;
        protected function clearValues(Address\Total $total)
            $total->setTotalAmount('subtotal', 0);
            $total->setBaseTotalAmount('subtotal', 0);
            $total->setTotalAmount('tax', 0);
            $total->setBaseTotalAmount('tax', 0);
            $total->setTotalAmount('discount_tax_compensation', 0);
            $total->setBaseTotalAmount('discount_tax_compensation', 0);
            $total->setTotalAmount('shipping_discount_tax_compensation', 0);
            $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
         * @param \Magento\Quote\Model\Quote $quote
         * @param Address\Total $total
         * @return array|null
         * Assign subtotal amount and label to address object
         * @param \Magento\Quote\Model\Quote $quote
         * @param Address\Total $total
         * @return array
        public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
            return [
                'code' => 'customfee',
                'title' => 'Custom Fee',
                'value' => 100
         * Get Subtotal label
         * @return \Magento\Framework\Phrase
        public function getLabel()
            return __('Custom Fee');

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


  • Martin Peasley
    What happens when your order fails for some reason and a new quote is created? Does the custom fee get added to the new quote that gets generated?
  • vinit kumar
    HI can you explain why you have used Sales.xml
  • Vinit
    Is above code For extra fee will work with PayPal Express Checkout ?
  • Arsalan Ajmal
    Hi Thanks for your article It works great. Now can you help me about how to set my custom price at cart subtotal in checkout summary? As I want to change cart subtotal on fly after saving shipping rates information.
  • Display custom price fee on checkout cart and summary total in magento2
    […] How to add custom price/fee in order total magento2 […]
  • How to display custom price fee in sales order view page
    […] How to add custom price/fee in order total magento2 […]
  • Back to Top