Magento 2 : Add discount and tax in order programmatically

If you want to add discount and tax in the order programmatically, here is the code snippet to apply discount and tax in your custom code :

$subTotal = 150;
$taxRate = .0825;
$taxAmount = $subTotal * $taxRate;
$shippingAmount = 10;
$totalAmount =  $subTotal + $taxAmount + $shippingAmount;

$order->setSubtotal($subTotal);
$order->setBaseSubtotal($subTotal);
$order->setGrandTotal($totalAmount);
$order->setBaseGrandTotal($totalAmount);
$order->setTaxAmount($taxAmount);
$order->setDiscountAmount(0);

Hope this helps you guys!

Leave a Reply