ccrz.ccApiCoupon.apply

Verifies that a specific coupon code is valid for a specific shopping cart, and calculates the coupon's specified discount. The method executes in two steps. First, the method adds a coupon code to a cart that doesn't already have a coupon applied, and creates the ccrz__E_CartCoupon__c junction object. Then, the method applies the added coupon by calculating discounts and verifying that the coupon criteria remains satisfied, even as cart items, quantities, and prices change.

Compatibility

This reference applies to:

Release Managed Package Version API Version
B2B Commerce for Visualforce Winter ’21 4.13 12
B2B Commerce for Visualforce Spring ’20 4.12 11
B2B Commerce for Visualforce Summer ’19 4.11 10

Signature

global static Map<String, Object> apply(Map<String, Object>)

Service Layer Classes

Logic Service Provider
ccrz.ccLogicCouponApply

Inputs (Required)

Map<String, Object> that must include the following required keys:

ccrz.ccApi.API_VERSION
The version of the B2B Commerce for Visualforce API to reference for the method call. We recommend that you use the ccrz.ccApi.CURRENT_VERSION constant whenever possible, and only reference a specific version for compatibility if necessary.

If this key isn't specified, the method returns a ccrz.ccApi.NoApiVersionException.

Note

Include either of the following keys, but not both. If you don't include either key, the method returns a ccrz.ccApi.MissingInputException.

ccrz.ccApiCart.CART_ID
String that specifies the Salesforce ID of the cart that you want to add or apply a coupon to.
ccrz.ccApiCart.CART_ENCID
String that specifies the encrypted ID of the cart that you want to add or apply a coupon to.

Inputs (Optional)

The input map can also include the following keys:

ccrz.ccApiCoupon.CART_HEADER
ccrz__E_Cart__c record that you want to add or apply a coupon to.
ccrz.ccApiCoupon.CART_ITEMS
List<ccrz__E_CartItem__c> of the cart items for the ccrz.ccApiCoupon.CART_HEADER, if specified.
ccrz.ccApiCoupon.CODE
String that specifies a coupon code to add to the cart.
ccrz.ccApiCoupon.SKIP_APPLY_CALC
Boolean
Value Usage
true Skip the ccrz.ccLogicCouponApply.applyCoupons method in the logic service provider. This method adds the coupon to the cart, but doesn't apply the coupon's specified discount.
false (default) Execute the ccrz.ccLogicCouponApply.applyCoupons method to apply the coupon's discount after adding the coupon to the cart.

If you specify either ccrz.ccApiCoupon.CART_HEADER or ccrz.ccApiCoupon.CART_ITEMS, make sure to include the other key, as well. When these keys are specified, the logic chain already has the required cart data. The ccrz.ccLogicCouponApply logic service provider skips the retrieveCartData method. Otherwise, the retrieveCartData executes, and chains a call to the ccrz.ccApiCart.fetch method.

Tip

Outputs

Map<String, Object> that can include the following keys:

ccrz.ccApi.API_VERSION
Integer that indicates which API version was used for the query.
ccrz.ccApi.SUCCESS
Boolean
Value Usage
true The call completed.
false The call encountered errors.

B2B Commerce for Visualforce doesn't always return an exception for any errors that can occur. When this value is false, consider rolling back the API transaction to a previous savepoint.

Tip

Examples

Add and apply a coupon to a cart whose encrypted ID you specify.

Boolean wasSuccessful = false;

Map<String, Object> couponToApplyToCart = new Map<String, Object>{
    ccrz.ccApi.API_VERSION => ccrz.ccApi.CURRENT_VERSION,
    ccrz.ccApiCart.CART_ENCID => 'Cart_Encrypted_ID',
    ccrz.ccApiCoupon.CODE => 'someCouponCode'
};
  
try {
    Map<String, Object> applyCouponResult = ccrz.ccApiCoupon.apply(couponToApplyToCart);
    wasSuccessful = (Boolean)applyCouponResult.get(ccrz.ccApi.SUCCESS);
} catch (Exception e) {
     // Error handling...
}

Add a coupon to a cart whose encrypted ID you specify, but skip the logic that determines whether an added coupon can apply to the cart.

Boolean wasSuccessful = false;

Map<String, Object> couponToAddToCart = new Map<String, Object>{
    ccrz.ccApi.API_VERSION => ccrz.ccApi.CURRENT_VERSION,
    ccrz.ccApiCart.CART_ENCID => 'Cart_Encrypted_ID',
    ccrz.ccApiCoupon.CODE => 'someCouponCode',
    ccrz.ccApiCoupon.SKIP_APPLY_CALC => true
};
  
try {
    Map<String, Object> addCouponResult = ccrz.ccApiCoupon.apply(couponToAddToCart);
    wasSuccessful = (Boolean)addCouponResult.get(ccrz.ccApi.SUCCESS);
} catch (Exception e) {
     // Error handling...
}