Question

I'm using Magento version 2.3.4, no matter what state I select on the cart page, the tax will only be calculated by Michigan tax rate.

Here are the Tax rates,

Screnshot 1

Here is the video for better understanding:- https://nimb.ws/248uAG

Tax is being calculated on,

Tax amount = (Subtotal+Total Extrafee)*Tax rate

I'm overriding Magento\Tax\Model\Sales\Total\Quote\Tax class.

Vendor/Module/etc/di.xml

<preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="Vendor\Module\Model\Sales\Total\Quote\Tax" />

What I want to do is getCustomerTaxClassId() of selected state in the Vendor\Module\Model\Sales\Total\Quote\Tax.php file.

Right now I'm getting $classid = $quote->getCustomerTaxClassId(); as "3" which is for Michigan.

Vendor/Module/Model/Sales/Total/Quote/Tax.php

<?php

 $enabled = $this->helperData->isModuleEnabled();
 if ($enabled) {
            $fee = $this->helperData->getExtrafee();
            $extraFeeTax = 0;
            $subtotal = $quote->getSubtotal();
            if (isset($rate[0])) {
                $fee = $subtotal + $fee;
                $extraFeeTax = ($rate[0] * $fee) / 100;
            } else {
                $classid =$quote->getCustomerTaxClassId();
                $taxRates = $this->_taxModelConfig->getCollection()->getData();

                $logger->info('classid');
                $logger->info($classid);

                $array_rates =array_column($taxRates, 'tax_calculation_rate_id');
                $tax_id = array_search($classid, $array_rates);
                if (isset($taxRates[$tax_id])) {
                    $taxData = $taxRates[$tax_id];
                    $rate = $taxData['rate'];

                    // $logger->info($taxData);
                    $logger->info($rate);
                }
                $fee = $subtotal + $fee;
                $extraFeeTax = ($rate * $fee) / 100;
            }
            $amount = $amount + $extraFeeTax;
            $total->setTotalAmount('tax', $amount);
            $total->setExtraTax($extraFeeTax);
            $quote->setExtraTax($extraFeeTax);
        }

I'm adding the log,

2020-05-18T06:09:27+00:00 INFO (6): Start
2020-05-18T06:09:27+00:00 INFO (6): classid
2020-05-18T06:09:27+00:00 INFO (6): 3
2020-05-18T06:09:27+00:00 INFO (6): array (
  'tax_calculation_rate_id' => '3',
  'tax_country_id' => 'US',
  'tax_region_id' => '33',
  'tax_postcode' => '*',
  'code' => 'US-MI-*-Rate 1',
  'rate' => '15.0000',
  'zip_is_range' => NULL,
  'zip_from' => NULL,
  'zip_to' => NULL,
)
2020-05-18T06:09:27+00:00 INFO (6): 15.0000

If anyone has any ideas, please let me know.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top