Featured: Magento 2 Pools

Latest article one-liner

Read the full article

Ask Me Anything

Got a webdevelopment related question?
Ask me, and I'll generate an in-depth article about it.

Ask a question

Long Reads

Grab a cup of coffee and let me tell you something …

* grabs coffee *

Latest article:


How to get the product price including and excluding tax in Magento 2

How to, Magento 2 ,

It seems like a simple task: get the price of a product in Magento including and/or excluding tax in Magento 2. I mean, how hard can it be? Well, to answer this question you must know that there are certain parameters in Magento 2 that determine the tax rate of a product:

  • The configuration setting if your catalog prices are including or excluding tax.
  • The current store (because different stores can have different countries that can have different tax rates).
  • The current customers’ customer group (because different customer groups can have different tax rates).

There are also some other parameters, like if the product has a special price or not, but for the sake if simplicity we’ll leave those out of the equation.

Step 1: Determine the products’ tax class

This first step is easy. Just load the product and ask for it’s tax_class_id:

This is the tax class that is applied to the product. Beware: the tax class is not the tax rate. The tax class simply states the ‘tax group’ the product is set to. Like ‘Taxable Goods’ for example, or ‘Food’ and ‘Non Food’. The rates applied to these classes are configured in your store under Store > Taxes.

Step 2: Determine the tax rate

This step might seem a bit more difficult, but it’s actually very easy due to the Service Contracts that are available. In this case we can use the Magento\Tax\Api\TaxCalculationInterface which has a getCalculatedRate()-method that returns our tax rate:

This method also has 2 additional parameters: customer ID and store ID, so we can use those if we want to have a tax rate for a specific customer or store. If you omit them, the current customer and store are used.

Step 3: Determine if the catalog prices are including or excluding tax

The third step is to check the configuration to determine if the catalog prices are including or excluding tax:

At this point, we already know our products’ price excluding tax. So to wrap it all up:

Step 4: Calculate the price including tax

This part is easy:


A simple example, yet it shows a great practical use of Service Contracts and the various parameters of a typical Magento 2 installation that can make this a bit more of a difficult task. Here’s the complete method including it’s dependencies to wrap it all up:

This example returns an array with both the including and excluding prices for the current customer / store, regardless of what the configuration setting is. This example sure could be a bit more improved, for example: have a method for getting either the including or excluding price instead of return an associated array, but for this example this will do just fine.

Featured categories:

Magento - It's one of the most used e-commerce solutions out there today. Here's my 2 cents about it. (53 articles)

Last post in this category:

Customer Tax and Discount Rules

Webdevelopment - Making the internet a more beautiful place day by day since 2006. (54 articles)

Last post in this category:

Regexes for quickly mocking stuff

Long reads - Sit back, grab a coffee and enjoy some of my more in-depth articles about stuff that moves me. (7 articles)

Last post in this category:

Customer Tax and Discount Rules