Create a discount campaign
Outcome: a code-backed cart discount tested against both qualifying and non-qualifying carts before activation.
Prerequisites
Choose the store context, currency, eligibility rule, target, value, validity window, usage limits, priority, and combination policy. Read Cart discounts and targets and validate the predicate in its cart context.
1. Create the discount
Build the input from CartDiscountCreateInput. For the fixture, use a narrow test window and a cart-total predicate with a clear boundary. Choose an absolute or relative value and explicit target; do not copy a product predicate into this field.
2. Add a code
Call cartDiscountCodeCreate for the returned discount with a test code and conservative usage limits. Query the discount and code to verify their relationship before exposing the code.
3. Test both outcomes
Create one cart below the threshold and one above it in the same store/currency context. Add the code using cartDiscountCodeAdd. The ineligible cart can accept a syntactically valid code without producing the expected application if the underlying rule does not match; inspect discountApplications, line or shipping applications, subtotal, tax, and total.
4. End and verify
End the campaign by updating its validity or deleting the intended rule/code through supported Admin operations. Re-query both Admin configuration and a fresh Storefront cart. Existing orders retain their recorded discount snapshot.
Reference: CartDiscount and CartDiscountCode.