9
  • I want to add a tax on custom fee. tax calculation is perfect and gets tax amount but it's not set in TAX files in quote_address in the database.

    public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ){
    
      $total->setTaxAmount($total->getTaxAmount()+$taxAmount);
      $total->setBaseTaxAmount($total->getBaseTaxAmount()+$taxAmount); 
    }
    
  • I try to set tax Amount in collect method but it's not set.

  • I want to same like this thiscode. it's Magento1 but I want Magento 2

Prince Patel
  • 22,708
  • 10
  • 97
  • 119
  • Try to follow the following to add custom fee to your subtotal https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2/93349 – Abhinav Singh Sep 27 '17 at 04:49

2 Answers2

1

You may please use below code to set tax amount in quote address and quote tables:

$quote->setTaxAmount($total->getTaxAmount() + $taxAmount);
$quote->setBaseTaxAmount($total->getBaseTaxAmount() + $taxAmount);

$address = $shippingAssignment->getShipping()->getAddress();
$address->setTaxAmount($total->getTaxAmount() + $taxAmount);
$address->setBaseTaxAmount($total->getBaseTaxAmount() + $taxAmount);

I hope this help to you. If you have any further query feel free to ask.

Happy to help!

Thanks,

Pratik Navapara
  • 1,160
  • 11
  • 27
0

Instead of a call to $total->setTaxAmount() you need to use addTotalAmount:

$total->addTotalAmount('tax', $amount);
$total->addBaseTotalAmount('tax', $baseAmount);
Michiel
  • 242
  • 1
  • 6