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


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" />


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


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


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.

  • Overriding Magento\Tax\Model\Sales\Total\Quote\Tax I able to change the tax but it not updated in Invoice. Any idea?
    – user44765
    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 Jul 23, 2020 at 12:00

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
) {
    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 =
        $this->processShippingTaxInfo($shippingAssignment, $total, $shippingTaxDetails, $baseShippingTaxDetails);

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

    //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

  • 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
    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
    Sep 22, 2020 at 13:28
  • is there any use of code $x= $quote->getItems(); and following lines under foreach loop?
    – Mukesh
    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
    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
    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.