Magento handles product tax calculation on its own. But there are times when we need manual tax calculation for our products.
$_item->getTaxAmount() gives us the tax amount applied on the product. But there is yet another way to calculate tax.
Click here to list all taxes in magento 2
Load your product
$productId = 10; // for example $_product = Mage::getModel('catalog/product')->load($productId);
Next we will calculate the tax rate percent. But for that we will require customer address, store information and product tax class id.
// product tax class id $tax_class_id = $_product->getTaxClassId(); //store $store = Mage::app()->getStore(); //store // address $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping(); if ($customerAddressId){ $address = Mage::getModel('customer/address')->load($customerAddressId); }
Get product tax percent
$taxCalculation = Mage::getModel('tax/calculation'); $request = $taxCalculation->getRateRequest($address, null, null, $store); $percent = $taxCalculation->getStoreRate($request->setProductClassId($tax_class_id), $store);
Calculate product tax amount
// get product price including tax $price = Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice()); $tax_amount = round($price/(100+$percent)*$percent,2);
$tax_amount has been rounded upto 2 decimal places.
Happy coding 🙂
Be the first to comment.