Example: Show Product Variant
Show original and sale prices.
On this page
In this guide, you'll learn how to display a product variant's sale price, with a full React example.
Tip: Refer to the Show Product Variant's Price guide to learn how to display a product variant's regular price.
Check if a Price is a Sale
A product variant's price can be a sale price or a regular price. The sale price is set by the merchant in a price list.
To check if a product variant's price is a sale price, check whether the variant's calculated_price.calculated_price.price_list_type field is equal to sale:
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.
Good to know: The sale price may be available for all customers, or only for some customers. If you send requests as an authenticated customer, the price list is automatically applied based on the customer's group.
Only price lists of type sale produce a comparison price that you can display as a strikethrough. If the price comes from a price list of type override, the calculated_price.original_amount field equals the calculated_amount, so there's no separate original price to cross out. Use the override type when you want to replace a customer segment's price without showing a discount.
Display Original and Discount Amounts
If the price is a sale price, the original price is available in the variant's calculated_price.original_amount field:
Tip: Find the implementation of the formatPrice function in the Show Product Variant's Price guide.
ts
You define three variables:
salePrice: The sale price of the variant.originalPrice: The original price of the variant.discountedAmount: The difference between the original and sale prices.
You can use the original price either to display it or calculate and display the discounted amount.
Full React Example
For example, to display the sale price and the original price 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 to the request retrieving the product. This retrieves for every variant a new
calculated_pricefield holding details about the variant's price.- 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.
- Define an
isSalememo variable that determines whether the chosen variant's price is a sale price. You do that by checking if the value of the variant'scalculated_price.calculated_price.price_list_typefield issale. - Define an
originalPricememo variable that, ifisSaleis enabled, has the formatted original price of the chosen variant. The variant's original price is in thecalculated_price.original_amountfield. - If
isSaleis enabled, show a message to the customer indicating that this product is on sale along with the original price.