How to Get Product Price Including Tax in Magento 2
The default Magento 2 displays the original product price as configured from the admin panel. It does not include the tax amount that a customer has to pay. Your customers might feel deceived to find out the tax amount only after going through the selection process and adding the product to the cart.
This makes it important to show the payable tax while they’re still exploring the product on your Magento 2 store.
Also, when you are offering your Magento 2 store products on a third-party platform/marketplace, say, Facebook store, the original product price is displayed. Now, if you want to display the price, including the tax, you need to programmatically fetch the product price with the tax amount added.
Or simply use the Magento 2 Facebook Shop Integration extension to not worry about the below code and sell your Magento 2 store products on the Facebook shop!
For now, here’s the solution to get product price including tax in Magento 2.
The solution to Get Product Price Including Tax in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace Meetanshi\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Catalog\Model\Product; use Magento\Catalog\Helper\Data as TaxHelper; class Data extends AbstractHelper { protected $productModel; protected $taxHelper; public function __construct( Context $context, Product $productModel, TaxHelper $taxHelper ) { $this->productModel = $productModel; $this->taxHelper = $taxHelper; parent::__construct($context); } public function getProductPrice($productId) { $product = $this->productModel->load($productId); $price = $this->taxHelper->getTaxPrice($product, $product->getFinalPrice(), true); return $price; } } |
If you have any doubts about this programmatic method to get product price including tax in Magento 2, please ask me in the comment section below. I’d be happy to help you.😊
Also, for good karma, do share the solution with Magento Community and other Magento developers. Thank you!
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
How to Change Currency Symbol Position in Magento 2
How to Add Custom Field in Checkout Page Below Payment Method List in Magento 2
Next