Skip to main content

Discounts API

The Discounts API provides access to discount codes and allocations applied to the checkout. Use this API to display active discounts, apply new discount codes, and remove existing ones.

  • Display applied discount codes: Show the buyer which discount codes are currently active on the checkout.
  • Apply a discount code: Let buyers enter a discount code and submit it programmatically.
  • Show discount breakdowns: Display how discounts are distributed across individual line items.
Support
Targets (31)

The shopify global object provides discount data for the current checkout. Access the following properties on shopify to read applied discount codes and allocations. Available to purchase extension targets.

Anchor to discountAllocations
discountAllocations
<[]>
required

The discount allocations applied to the entire cart, including automatic discounts, code-based discounts, and custom discounts from Shopify Functions. Each allocation indicates how much was discounted and how the discount was triggered.

Anchor to discountCodes
discountCodes
<[]>
required

The discount codes currently applied to the checkout. The list is empty if no discount codes have been applied. Use applyDiscountCodeChange() to add or remove codes.

The shopify global object provides methods to modify discount data. Access the following methods on shopify to apply or remove discount codes. Available to purchase.checkout extension targets.

Anchor to applyDiscountCodeChange
applyDiscountCodeChange
(change: ) => Promise<>
required

Adds or removes a discount code on the checkout. The returned promise resolves when the change has been applied by the server, and the discountCodes property updates with the new state.

Caution

> See security considerations if your extension retrieves discount codes through a network call.

Note

This method returns an error if the cart instruction discounts.canUpdateDiscountCodes is false, or the buyer is using an accelerated checkout method, such as Apple Pay or Google Pay.


  • Provide your own feedback on errors: applyDiscountCodeChange resolves with a type of 'success' or 'error'. The error message is for debugging only and isn't localized, so display your own buyer-facing feedback (for example, "That code didn't work. Check the code and try again.").
  • Treat discount codes as case-insensitive: The discount system treats SAVE10 and save10 as the same code. Normalize the display in your UI to avoid confusion.

  • Discount code changes aren't available when the buyer uses an accelerated checkout method such as Apple Pay or Google Pay.
  • Extensions can add or remove discount codes, but can't modify automatic discounts or custom discounts applied by Shopify Scripts.

Was this page helpful?