Example: Show Product Variant
Show prices with tax included or excluded.
On this page
In this guide, you'll learn how to show a product variant's price with taxes, with a full React example.
Tip: Refer to the Show Product Variant's Price guide to learn about the general approach to display a product variant's price.
Retrieve Variant's Price with Tax
To retrieve a product variant's price with taxes, you must pass the region_id and country_code query parameters:
Tip: Learn how to install and configure the JS SDK in the Getting started guide.
ts
You pass the selected region's ID and the code of its first country as query parameters to the Get Product API route.
Tip: You can also allow the customer to choose their country by showing them the countries in region.countries in a select input, and storing the selected country's code in the local storage.
Display Variant's Price with Taxes
After passing the region_id and country_code as query parameters when retrieving the product, each variant's price object will have a calculated_amount_with_tax property to indicate the price taxes applied:
Tip: Find the implementation of the formatPrice function in the Show Product Variant's Price guide.
ts
Where selectedVariantPrice is either the variant the customer selected or the cheapest variant. Learn more about how to set the selected variant for pricing in the Show Product Variant's Price guide.
Tax-Related Properties in Calculated Price
Aside from the calculated_amount_with_tax property, a variant's calculated_price object has the following properties related to taxes:
calculated_amount_without_tax: The calculated amount without taxes. This may be useful if you want to show the customer the price without taxes, and show the applied taxes separately. In that case, you can subtractcalculated_amount_without_taxfromcalculated_amount_with_taxto get the tax amount.is_calculated_price_tax_inclusive: Whether thecalculated_amountproperty includes taxes. If enabled, you can display thecalculated_amountproperty instead ofcalculated_amount_with_tax.
Learn more about the calculated_price object in the Retrieve Product Variant's Prices guide.
Full React Example
For example, to display the price with taxes in a React-based storefront:
Tip: - This example uses the useRegion hook defined in the Region React Context guide to retrieve the selected region's currency code.
- Learn how to install and configure the JS SDK in the Getting started guide.
tsx
In this example, you:
- Use the
useRegionhook defined in the previous Region React Context guide to retrieve the selected region's currency code. This is necessary to format the variant's price. - Pass the pricing query parameters, including the selected region's ID and the code of its first country, to the request retrieving the product. This retrieves for every variant a new
calculated_pricefield holding details about the variant's price and taxes.- You can pass other pricing query parameters for more accurate pricing. Refer to the Retrieve Product Variant's Prices guide for more information.
- Choose the variant to show its price:
- If there's a selected variant, choose it.
- If there isn't a selected variant, retrieve and choose the variant with the cheapest price.
- Display the selected variant's price with taxes by formatting its price's
calculated_amount_with_taxproperty.