I want to add custom tax amount to checkout cart programmatically.

Here is the example.

Cart Old Tax = 4.21

Custom Tax = 2

New Tax = 4.21 + 2 = 6

Check the screenshot below.

enter image description here

I need this done programmatically.

  • Where your custom amount come from? – Toan Nguyen Jan 10 '17 at 9:31
  • @ToanNguyen that I fetch from the session, My point is how can add that value to tax. – Dhiren Vasoya Jan 10 '17 at 9:52
  • @DhirenVasoya, how can we add custom tax price for the product when added to cart, here is my question,magento.stackexchange.com/questions/274520/… – jafar pinjar May 14 '19 at 11:25
  • @DhirenVasoya, below solution i tried,this is showing cart but not calculating.. – jafar pinjar May 14 '19 at 16:41
  • @DhirenVasoya - did you get the solution to this problem ? – Manashvi Birla Jul 17 '19 at 13:33

You can observe to the event sales_quote_address_collect_totals_after and achieve it. For this, you need to setup a module and configure an event. Let's say our module is MStack_Exchange.

File : app\code\MStack\Exchange\etc\events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_address_collect_totals_after">
        <observer name="changeTaxTotal" instance="MStack\Exchange\Observer\ChangeTaxTotal"/>

File : app\code\MStack\Exchange\Observer\ChangeTaxTotal.php

namespace MStack\Exchange\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;

class ChangeTaxTotal implements ObserverInterface
    public $additionalTaxAmt = 2;

    public function execute(Observer $observer)
        /** @var Magento\Quote\Model\Quote\Address\Total */
        $total = $observer->getData('total');

        //make sure tax value exist
        if (count($total->getAppliedTaxes()) > 0) {
            $total->addTotalAmount('tax', $this->additionalTaxAmt);

        return $this;

The important call here is : $total->addTotalAmount('tax', $this->additionalTaxAmt);. This will add 2 with the existing tax amount and I think that is what you need in your case. So what you need to do is, replace $this->additionalTaxAmt with your tax buffer value.

The event sales_quote_address_collect_totals_after fires just after total calculations had been happend and hence thus become perfect place to play around.

If you are curious to know where this total calculations are happening, then you need to look into Magento\Quote\Model\Quote\TotalsCollector::collect() and Magento\Quote\Model\Quote\TotalsCollector::collectAddressTotals() methods.


@Dhiren Vasoya

Use these lines as well.

$total->addBaseTotalAmount('tax', $this->additionalTaxAmt);
$total->setGrandTotal((float)$total->getGrandTotal() + $this->additionalTaxAmt);
$total->setBaseGrandTotal((float)$total->getBaseGrandTotal() + $this->additionalTaxAmt);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.