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
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);
}
$this->processCustomTax($quote);
return $this;
}
-
-
i am new to magento 2 can you please elaborate this answer with detail..because i tried but ..– sanganApr 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. 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– sanganApr 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. 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
) {
$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;
}
The above code works for me.
-
-
it works for me. is there anything else overriding the tax at checkout? Please check. Nov 25, 2020 at 20:34
-