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.

  • Do you find how to set tax amount? if yes than please post it how to do this? – Newbie Apr 25 '17 at 11:11

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;
| improve this answer | |
  • ok i will try now – sangan Apr 18 '17 at 12:39
  • i am new to magento 2 can you please elaborate this answer with detail..because i tried but .. – sangan Apr 19 '17 at 6:39
  • I do not have the detail of the implementation sorry, I just share you an approach. Let's wait other answers. – Franck Garnier Apr 19 '17 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 Apr 19 '17 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. – Franck Garnier Apr 19 '17 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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