cancel
Showing results for 
Search instead for 
Did you mean: 

How to override Tax Calculation

How to override Tax Calculation

Hello All,

 

There is two taxes in my magento shop, let it be tax1 and tax2.

I want to calculate tax2 as percenage of all other prices including tax1.

How can I achieve this ?

Thank you

4 REPLIES 4

Re: How to override Tax Calculation

Hi @sayanth_k 

You can customize the code as well for same : In your di.xml, add as below:

<preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="<yournamespace>\<yourModule>\Model\Sales\Total\Quote\Tax"/>

Now create your class file Tax.php and add below code:

namespace <yournamespace>\<yourModule>\Model\Sales\Total\Quote;

class Tax extends \Magento\Tax\Model\Sales\Total\Quote\Tax

{
    /* override code here */
    public function __construct(
        /* add dependency classes here */    ) {
        parent::__construct(
            /* parent class objects here */
        );
    }

    /**
    * Custom Collect tax totals for quote address
    *
    * @param Quote $quote
    * @param ShippingAssignmentInterface $shippingAssignment
    * @param Address\Total $total
    * @return $this
    */
    public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {

       /* your calculation here goes here */
        $total->setTaxAmount($set_your_tax_here);

        return $this;
    }

}

Hope this helps!

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

Re: How to override Tax Calculation

@Bhanu PeriwalCould you please explain more how can I change the tax2 only ?

Re: How to override Tax Calculation


@sayanth_k wrote:

@Bhanu PeriwalCould you pleas Kodi  explain more how can I change the tax2 only ?


I want to calculate tax2 as percenage of nox  all other prices including tax1.

Re: How to override Tax Calculation

@Bhanu Periwal 

Hi, thanks for the code, when I have it all saved I am getting the following error on product page: 

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Magento\Quote\Model\Quote\Address\Total\Collector

in my di.xml I have this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley SurprisedbjectManager:etc/config.xsd">
<preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="Tba\Custom\Model\Sales\Total\Quote\Tax"/>
</config>

Screen shot of my folder structure: https://jmp.sh/tz1DDeX

Screen shot of my Tax.php: https://jmp.sh/ViQVk0j

Any idea what I am missing?