3

I need the tax to be added from external source. But I have hard time to find when should I alter the tax. Any idea?

2 Answers 2

4

A good starting point would be to create a class preference for Magento\Tax\Model\Sales\Total\Quote\Tax:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager:etc/config.xsd">
    <preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="Taxjar\SalesTax\Model\Tax\Sales\Total\Quote\Tax" />
</config>

https://github.com/taxjar/taxjar-magento2-extension/blob/develop/etc/di.xml#L20

You'd then extend the Tax class and override the collect method:

https://github.com/taxjar/taxjar-magento2-extension/blob/develop/Model/Tax/Sales/Total/Quote/Tax.php#L107

In TaxJar's extension, we pass in the tax by line item by overriding the mapItem method inherited from CommonTaxCollector:

https://github.com/taxjar/taxjar-magento2-extension/blob/ba687328ce1cb91030617a74394b318649fc50f3/Model/Tax/Sales/Total/Quote/Tax.php#L256

There's certainly a lot more to this if you want to do it the right way. I recommend studying TaxJar's source code and looking at the relevant classes in Magento 2 to see how it's done.

3
  • Overriding Magento\Tax\Model\Sales\Total\Quote\Tax I able to change the tax but it not updated in Invoice. Any idea?
    – user44765
    Commented Dec 8, 2016 at 5:44
  • Hi, @Jake we have used tax jar plugin but for specific orders (Ex:- programatically creating orders) should we would like to calculate 7.75% tax fixed how to do that? please advise Commented Jul 23, 2020 at 12:00
  • magento.stackexchange.com/questions/317989/… Commented Jul 23, 2020 at 12:01
1

I rewrite the Tax classname and it works for me.

    <preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="BA\Vertex\Rewrite\Magento\Tax\Model\Sales\Total\Quote\Tax"/>

Rewrite class

 public function collect(
    \Magento\Quote\Model\Quote $quote,
    \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
    \Magento\Quote\Model\Quote\Address\Total $total
) {
    
    $this->clearValues($total);
    if (!$shippingAssignment->getItems()) {
        return $this;
    }

    $x= $quote->getItems();
    if (isset($x)) {
        foreach ($x as $item) {
            $g = $item;
            $y = $item->getSku();
            $y = $item->getProductId();
        }
    }
    
    $baseTaxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, true);
    $taxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, false);

    //Populate address and items with tax calculation results
    $itemsByType = $this->organizeItemTaxDetailsByType($taxDetails, $baseTaxDetails);
    if (isset($itemsByType[self::ITEM_TYPE_PRODUCT])) {
        $this->processProductItems($shippingAssignment, $itemsByType[self::ITEM_TYPE_PRODUCT], $total);
    }

    if (isset($itemsByType[self::ITEM_TYPE_SHIPPING])) {
        $shippingTaxDetails = $itemsByType[self::ITEM_TYPE_SHIPPING][self::ITEM_CODE_SHIPPING][self::KEY_ITEM];
        $baseShippingTaxDetails =
            $itemsByType[self::ITEM_TYPE_SHIPPING][self::ITEM_CODE_SHIPPING][self::KEY_BASE_ITEM];
        $this->processShippingTaxInfo($shippingAssignment, $total, $shippingTaxDetails, $baseShippingTaxDetails);
    }

    //Process taxable items that are not product or shipping
    $this->processExtraTaxables($total, $itemsByType);

   $total->setTaxAmount(2000);
   $total->setGrandTotal($total->getBaseGrandTotal()+2000);
   
    //Save applied taxes for each item and the quote in aggregation
    $this->processAppliedTaxes($total, $shippingAssignment, $itemsByType);

    if ($this->includeExtraTax()) {
        $total->addTotalAmount('extra_tax', $total->getExtraTaxAmount());
        $total->addBaseTotalAmount('extra_tax', $total->getBaseExtraTaxAmount());
    }

    return $this;
}

enter image description here

8
  • Did it work for you on the checkout page cart summary? For me it adds the value of tax from the products level tax also in the final tax
    – Mukesh
    Commented Sep 22, 2020 at 13:25
  • yes it is working the checkout page summary. I have checked the emails and admin everywhere it is updating correctly
    – Liz Eipe C
    Commented Sep 22, 2020 at 13:28
  • is there any use of code $x= $quote->getItems(); and following lines under foreach loop?
    – Mukesh
    Commented Sep 22, 2020 at 13:35
  • 1
    I am not sure from where you are trying to get the tax amount. I need to get the tax amount from the webservice. So I need to iterate through the cart items and get product id for fetching corresponding tax amount from the webservice. thatsy I have added that foreach.
    – Liz Eipe C
    Commented Sep 22, 2020 at 13:43
  • I also want to get the Tax from an API. I have to pass all the product sku, qty and row total to API then I receive the Tax amount for the cart. The solution provided by you is working on the cart page for me. But on the order order summary page it is adding the tax values imported in the Magento to the value 2000. If you could suggest any solution it will be helpful.
    – Mukesh
    Commented Sep 22, 2020 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.