cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom amount to tax

Add custom amount to tax

Hi All,

 

I have two tax rules in my online show.

 

1. Tax 1

2. Tax 2

The tax calculation is based on the subtotal. But now I want to add custom amount with the above tax .

Ie, Tax 1 = Tax 1 + custom amount
     Tax 2 = Tax 2 + custom amount.

Where I need to change the logic inorder to achieve this ?

Any help is really appreciated.

Thank you

2 REPLIES 2

Re: Add custom amount to tax

Hi @sayanth_k 

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.

Create event.xml under customer module :

 

path: app\code\Vendor\ModuleName\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="Vendor\ModuleName\Observer\ChangeTaxTotal"/>
</event>
</config>

Create observer file :

 app\code\MStack\Exchange\Observer\ChangeTaxTotal.php
<?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;
    }
}

 

It may help you.

Problem Solved ? Please click on 'Kudos' & Accept as Solution!

 

 

Problem solved? Click Accept as Solution!

Re: Add custom amount to tax

@Bhanu PeriwalHi need to add extra amount to individual taxes, I think in your mentioned way I can only add extra amount to toal tax value right ?