HOW TO UPDATE TAX WITH CUSTOM VALUE IN CART OR CHECKOUT.My requirement was when customer click add to cart and he move to cart that time i want to update tax with some API so do the needful.


2 Answers 2


One approach is :

You can try to overide the default collect total tax function.

Add this dependency :

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

Create you own Quote Tax Collection function based on native one.

Add your logic inside the collect function.

Example :

 * Custom Collect tax totals for quote address
 * @param Quote $quote
 * @param ShippingAssignmentInterface $shippingAssignment
 * @param Address\Total $total
 * @return $this
 * @throws RemoteServiceUnavailableException
public function collect(
    Quote $quote,
    ShippingAssignmentInterface $shippingAssignment,
    Address\Total $total
) {
    $address = $shippingAssignment->getShipping()->getAddress();

    $storeId = $quote->getStoreId();

    if ($this->doNotUseCustomTax()
    ) {
        return parent::collect($quote, $shippingAssignment, $total);


    return $this;
  • ok i will try now
    – sangan
    Commented Apr 18, 2017 at 12:39
  • i am new to magento 2 can you please elaborate this answer with detail..because i tried but ..
    – sangan
    Commented Apr 19, 2017 at 6:39
  • I do not have the detail of the implementation sorry, I just share you an approach. Let's wait other answers. Commented Apr 19, 2017 at 7:10
  • ok,i am getting tax in my custom module and updated the tax trying to set with ""$total->setTaxAmount($taxAmount);"" but $total not getting value
    – sangan
    Commented Apr 19, 2017 at 7:26
  • Try to find how this module works : github.com/classyllama/ClassyLlama_AvaTax It is used to setup custom taxes for the US. Commented Apr 19, 2017 at 7:33
  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;

The above code works for me.

enter image description here

  • this is recalculated on checkout page and back to original tax. Commented Nov 25, 2020 at 17:22
  • it works for me. is there anything else overriding the tax at checkout? Please check.
    – Liz Eipe C
    Commented Nov 25, 2020 at 20:34
  • How you can calculate tax on rowSubtotal amount ? Commented Feb 17, 2022 at 4:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.