Reading list Switch to dark mode

    Modify Total on the checkout page in Magento 2

    Updated 18 July 2023

    Hello Friends!!

    In today’s blog, I will explain how we can modify the total on the checkout page.

    This blog is useful if you need to update the total based on any condition only on the checkout page.

    First of all, we will create a sales.xml file inside the, etc folder. You can simply use the below code in your sales.xml file.

    <?xml version="1.0" ?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
        <section name="quote">
            <group name="totals">
                <item name="updatetotal" instance="Webkul\Test\Model\Total\UpdateTotal" sort_order="225"/>
            </group>
        </section>
    </config>

    Now we will create a UpdateTotal.php file inside the model folder.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    You can simply use the below code in your UpdateTotal.php file and modify it accordingly.

    namespace Webkul\Test\Model\Total;
    
    class UpdateTotal extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
    {
    
    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);
            
            if ("You can define you condition here") {
                $extraCharge = 10; // you can use your value
                $total->setGrandTotal($total->getGrandTotal()+$extraCharge);
                $total->setBaseGrandTotal($total->getBaseGrandTotal()+$extraCharge);
            }else{
                $total->setGrandTotal($total->getGrandTotal());
                $total->setBaseGrandTotal($total->getBaseGrandTotal());  
            }
            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);
            $total->setSubtotalInclTax(0);
            $total->setBaseSubtotalInclTax(0);
        }
        
    }

    Now the Total will be updated based on your condition.

    That’s all about on how to update the total on the checkout page. Hope this will be helpful.

    This blog can be useful for various cart and payment-related operations including Magento 2 partial payment.

    If you have any questions please comment below, and we will try to respond to you.

    Thanks for visiting the Webkul blog! 🙂

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

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

    Back to Home