1

I created a module that creates an order programmatically and I can also set the custom price. This is part of my code:

 foreach($orderData['products'] as $product) { try { $quoteProduct = $this->productRepository->get($product['sku']); $this->logger->info(sprintf("Adding product to quote, id: %d, sku: %s, qty: %d", $quoteProduct->getId(), $quoteProduct->getSku(), $product['qty'])); $quoteItem = $quote->addProduct( $quoteProduct, intval($product['qty']) ); $quoteItem->setCustomPrice($product['amountPaid']); $quoteItem->setOriginalCustomPrice($product['amountPaid']); $quoteItem->getProduct()->setIsSuperMode(true); } catch(\Exception $e) { $orderResult->errorMessage = sprintf("ERROR: %s, failed to add product to quote, sku: %s, qty: %d. Check saleable quantity, product may be out of stock.", $e->getMessage(), $product['sku'], $product['qty']); $this->logger->critical($orderResult->errorMessage); return $orderResult; } } 

I would like to set the custom tax amount just like I am setting the custom price. Can this be done?

To clarify: I don't want Magento to calculate and apply the tax for the product. I want to override its default behavior and set the line item tax amount manually.

enter image description here

2 Answers 2

2
foreach($orderData['products'] as $product) { try { $quoteProduct = $this->productRepository->get($product['sku']); $this->logger->info(sprintf("Adding product to quote, id: %d, sku:%s, qty: %d", $quoteProduct->getId(), $quoteProduct- >getSku(), $product['qty'])); $quoteItem = $quote->addProduct( $quoteProduct, intval($product['qty']) ); // Set custom price $quoteItem->setCustomPrice($product['amountPaid']); $quoteItem->setOriginalCustomPrice($product['amountPaid']); // Set custom tax amount $customTaxAmount = $product['customTaxAmount']; // Ensure this exists in your data $quoteItem->setTaxAmount($customTaxAmount); $quoteItem->setBaseTaxAmount($customTaxAmount); // Ensure totals are calculated correctly $quoteItem->setRowTotal($product['amountPaid'] * $product['qty']); $quoteItem->setBaseRowTotal($product['amountPaid'] * $product['qty']); $quoteItem->setRowTotalInclTax(($product['amountPaid'] * $product['qty']) + $customTaxAmount); $quoteItem->setBaseRowTotalInclTax(($product['amountPaid'] * $product['qty']) + $customTaxAmount); // Ensure Magento does not override the values $quoteItem->getProduct()->setIsSuperMode(true); } catch(\Exception $e) { $orderResult->errorMessage = sprintf("ERROR: %s, failed to add product to quote, sku: %s, qty: %d. Check saleable quantity, product may be out of stock.", $e->getMessage(), $product['sku'], $product['qty']); $this->logger->critical($orderResult->errorMessage); return $orderResult; } } 
4
  • this worked, thanks. But now I am experiencing an odd issue. If I run a command line script to create the order it works perfectly fine, tax is 0. But if the order is created via a cronjob, I get a tax amount, despite me passing 0 as the custom tax amount. Any ideas why command line works as expected (0 tax), and through the cronjob it's not? Might it be with the AppState perhaps? Commented Mar 20 at 23:15
  • Ok, so I was wrong. The command line script was using a different address. I updated the script to match the address, and now I am getting a consistent result - the tax is still applied, even though I am passing 0 tax amount. Commented Mar 20 at 23:58
  • @MichaelMussulis Good to know it work for you. Commented Mar 21 at 6:27
  • Actually, I found the real cause of the discrepancy. The project is using AvalaraTax and that is being invoked to apply taxes. So the customTax is being ignored because of Avalara. If I disable AvalaraTax customTax works fine. I need to implement an override to stop Avalara from applying taxes for custom orders. Commented Mar 21 at 13:56
0
/** @var Magento\Quote\Model\Quote\Address\Total */ $total = $observer->getData('total'); $subtotal = $total['subtotal']; $customTax = ($subtotal * 9)/100; $total->setTotalAmount('tax', $customTax); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.