Cart Discounts
Learn about the recent changes to Cart Discounts, and how to apply them using the TypeScript or Java SDK.
After completing this page, you should be able to:
- Identify how the changes affect the TypeScript and Java SDKs.
- Apply the updated features using the TypeScript or Java SDK.
You are most likely familiar with the concept of Cart Discounts. It offers great functionality if you want to encourage your customers to put more in their cart or make sure they keep their line items in the cart in return for a free gift that is automatically added.
In 2023, there has been three significant updates to how you can work with Cart Discounts:
- Cart Discounts now support money values in high precision. This is very useful, especially in B2B scenarios, when you'd like to deal with precise amounts in sub-cents.
- The Cart's total price can now be used as a Cart Discount target. We're sure that you agree this is a lot more convenient to bring a Cart's total down than targeting all of the Cart's Line Items.
- Cart Discounts can now be even more precise and flexible with the added support for managing them in Stores. This also makes it possible to have more Cart Discounts defined in a multi-Store Project, if they do not require Discount Codes. Each Store can have up to a total of
100
active Cart Discounts, in addition to the100
global Cart Discounts. This means the total limit of active Cart Discounts without Discount Codes is100
+ (100
* number of Stores in a Project).
The following code sample shows how you can create a Cart Discount with the above-mentioned features.
import { apiRoot } from '../impl/apiClient.js'; // Update to map to your API rootconst discountDraft = {key: 'summer-sale-cart-discount11',name: {'en-GB': 'Summer Sale','en-US': 'Summer Sale','de-DE': 'Sommerschlussverkauf',},value: {type: 'fixed',money: [{type: 'highPrecision', //high precision money now supportedcurrencyCode: 'EUR',fractionDigits: 4,preciseAmount: 100011,centAmount: 1000,},{type: 'highPrecision', //high precision money now supportedcurrencyCode: 'USD',fractionDigits: 4,preciseAmount: 115012,centAmount: 1150,},{type: 'highPrecision', //high precision money now supportedcurrencyCode: 'GBP',fractionDigits: 4,preciseAmount: 80013,centAmount: 800,},],},cartPredicate: '1 = 1',target: {type: 'totalPrice', //we can now target the Cart total price},sortOrder: '0.544',stores: [{ key: 'the-good-store', typeId: 'store' }], //cart discounts can now be Store-specificstackingMode: 'StopAfterThisDiscount',isActive: true,requiresDiscountCode: false,};async function cartDiscountCreate(discountDraft) {try {const response = await apiRoot.inStoreKeyWithStoreKeyValue({ storeKey: 'the-good-store' }).cartDiscounts().post({ body: discountDraft }).execute();console.log('Success', JSON.stringify(response.body, null, 2));} catch (error) {console.log(JSON.stringify(error, null, 2));}}cartDiscountCreate(discountDraft);
If the code runs correctly, you should be able to see the new Cart Discount in the Merchant Center, like below.