Add a discount code using setDiscount

You can now apply a discount code to the cart using the existing setDiscount method. It accepts a discount_code key in addition to amount, type and description.

If a discount_code is provided, other fields will be ignored. If no code is provided, setDiscount expects at least an amount, with discount_description and type being optional.

Key Type Description
discount_code String The discount code should match the exact code defined on Shopify admin on the Discounts page.
amount Number The discount amount to be applied to the subtotal of the cart as a flat value or total percentage discount. flat discount amounts must be greater than 0.0. Discounts greater than the subtotal of the cart will be reduced to the subtotal amount. percent discount amounts must be between 0.0 and 1.0.
discount_description String A description of the discount being applied. Default: Discount.
type String The discount type to apply to the subtotal of the cart. Valid values: flat, percent. Default: flat.

Example:

cart.setDiscount({
  discount_code: "10Percent"
}, {
  success: function(cart) {
    ShopifyPOS.flashNotice("Successfully added discount to cart")
  },
  error: function(errors) {
    ...
  }
})

Errors

When a discount code is provided to setDiscount, Shopify POS will validate that code to determine if it can be applied to the cart. If the code can't be applied, an invalid code error will be returned.

In addition, attempting to call setDiscount with discount_code but no amount on older Shopify POS versions will return an error. If a discount_code and amount is provided, older Shopify POS clients will ignore the code and parse the amount as expected.

Backwards compatibility

Older versions of Shopify POS will not support the ability to setDiscount with a code. This means that it is up to you, the developer, to gracefully support older Shopify POS clients.

In order to determine whether the merchant's version of Shopify POS supports setDiscount with a code, we recommend calling setDiscount with only discount_code. If that version of Shopify POS does not support adding discounts using a code, an error will be returned. We then recommend calling setDiscount again with amount, type, and description. For example:

cart.setDiscount({
  discount_code: "10Percent"
}, {
  success: function(cart) {
    ShopifyPOS.flashNotice("Successfully added discount to cart")
  },
  error: function(errors) {
    cart.setDiscount({
      amount: 10.00,
      discount_description: "holiday sale",
      type: "percent"
    }, { ... })
  }
})

Contact

If you have any other questions, please contact us.

Sign up for a Partner account to get started.

Sign up