Skip to main content

B2C Checkout Orchestration - Apply Custom Discount

In the following recipe, we will walk through an add custom discount to cart flow that includes validating a user-supplied coupon code against an external promotions engine, and mapping that response back to the schema required by Elastic Path in order to add the custom discount. We will use Elastic Path Commerce Cloud APIs as well as an API mock service to serve in the place of external systems.

Overall Orchestration Flow

Here is the flow that we will be modeling in the DX Engine:

  • A shopper is viewing their cart.
  • They add a coupon code for a cart-level discount.
  • The code must be validated by the external promotions engine.
  • The engine's response is mapped to the schema model required by Elastic Path.
  • The discount is applied to the cart.

alt_text

Mapping Out DX Engine Components and Templates

Translating this to Conscia’s Orchestration Components, the Add Custom Discount Code flow consists of the following Components and Context values:

alt_text

When the shopper inputs a discount code on the View Cart page, the storefront sends off a query request to the DX Engine. It calls the Add Custom Discount to Cart component and supplies the discount code. The call looks like this:

POST {{engineUrl}}/experience/components/_query  
X-Customer-Code: {{customerCode}}
Authorization: Bearer {{dxEngineToken}}

{
"componentCodes": ["epcc-add-custom-discount-to-cart"],
"context": {
"discountCode" : "SAVE10",
"cartId" : "5634ab34o1"
}
}

This is the orchestration flow that is then executed by the DX Engine:

In order to validate the coupon code, the external promotions engine may require a number of pieces of information about the cart and the customer. This information may be available on the frontend already to pass through the Context; this information may be cached already in Conscia; or a fresh call may need to be made to the commerce engine. In this flow recipe, we will do the last of these options and assume the required information is not readily available.

After generating an implicit token (if necessary), the DX Engine performs a Get Cart query to pull back any and all relevant information for the external promotions engine to validate the coupon code. In this example, we need only the cart total, before taxes and before discounts.

Next a mock API service is called to represent the external promotions engine. We pass through the coupon code and the cart total. The promotions engine will determine if this is a valid code and that it applies to a cart with this total value. If true, it will pass back a response that includes the actual discount to apply to the cart.

In order to apply this discount to the cart, we next need to map the promotions engine response to the Elastic Path custom discount schema. A mapper component is used to take the response, pull out the necessary properties, and map them the properties expected by the Add Custom Discount call.

After generating a client credentials token, we can now make the Add Custom Discount call to apply this coupon code to the cart.

DX Engine Configuration Details

You will need to configure a connection to Elastic Path Commerce Cloud. For the mock calls to external systems for discounts, we have not created a connection and are just calling the web service directly from the Components. You would want to create a connection for your own external systems, whether that’s a promotions engine or ERP, etc.

Below are instructions on how to configure the Elastic Path Commerce Cloud connection and each of the Components discussed above.

Create a Connection to Elastic Path Commerce Cloud

  • Navigate to the Connections page (Settings --> Connections).
  • Click the Configure Connection button
  • Enter the following and click Submit:
FieldValue
Connection Codeepcc-connection
Connection NameElastic Path Commerce Cloud Connection
ConnectorUniversal API Connector
Base URLGet value from: Literal
https://useast.api.elasticpath.com
MethodGET

Create a Component to generate an implicit token

  • Navigate to the Experience Components page (Manage Experiences --> Experience Components)
  • Click the Create Experience Component button
  • Enter the following and click Submit:
FieldValue
Component Codeepcc-get-implicit-token
Component NameEPCC Get Implicit Token
No RulesChecked
Component TypeConscia - Universal API Connector

The DX Engine will create and prepare the Component for further configuration. You should see the new component appear in the Component listing.

  • Click Edit
  • Enter the following and click Submit
FieldForm TabValueDescription
ConnectionMainElastic Path Commerce Cloud Connection
Webservice PathMainGet value from: Literal
/oauth/access_token
MethodMainPOST
HeadersMainHeader: Content-Type
Value (Literal): application/x-www-form-urlencoded
BodyMainValue (Literal): client_id={{clientId}}&grant_type=implicit
CachedCachingChecked
Cache Time-to-liveCaching3600
Context Field EnrichmentsUpdate ContextContext Field: implicitToken
Expression: 'Bearer' + response.access_token

Create a Component to validate the discount code

  • Navigate to the Experience Components page (Manage Experiences --> Experience Components)
  • Click the Create Experience Component button
  • Enter the following and click Submit:
FieldValue
Component Codemock-validate-discount
Component NameMock Validate Discount Code
No RulesChecked
Component TypeConscia - Universal API Connector

The DX Engine will create and prepare the Component for further configuration. You should see the new component appear in the Component listing.

  • Click Edit
  • Enter the following and click Submit
FieldForm TabValueDescription
ConnectionMainN/A
Webservice PathMainGet value from: Literal
https://r7jqm.wiremockapi.cloud/inventory/validate
Use any API mocking service of your choice.
MethodMainPOST
BodyMainGet value from: JS Expression
{"code" : "${contextField('discountCode')}", "amount": "${componentResponse('epcc-get-cart').data.meta.display_price.without_discount.amount}"}

Create a Component to map custom discount schema

  • Navigate to the Experience Components page (Manage Experiences --> Experience Components)
  • Click the Create Experience Component button
  • Enter the following and click Submit:
FieldValue
Component Codediscount-mapper
Component NameMapper - Discounts
No RulesChecked
Component TypeConscia - Object Mapper

The DX Engine will create and prepare the Component for further configuration. You should see the new component appear in the Component listing.

  • Click Edit
  • Enter the following and click Submit

Under the Object Maps section of the form click the + button to add a new Object Map.

FieldValue
Source DataGet value from: Component Response
Mock Validate Discount Code
Expression TypeJavascript
Schematype = 'custom_discount'
external_id = data.id
discount_engine = 'External Discount Engine'
amount = -(data.discount.amount_off)
description = data.campaign
discount_code = 'data.code

The schema to look like this:

alt_text

Create a Component to generate an client credentials token

  • Navigate to the Experience Components page (Manage Experiences --> Experience Components)
  • Click the Create Experience Component button
  • Enter the following and click Submit:
FieldValue
Component Codeepcc-get-client-credentials-token
Component NameEPCC Get Client Credentials Token
No RulesChecked
Component TypeConscia - Universal API Connector

The DX Engine will create and prepare the Component for further configuration. You should see the new component appear in the Component listing.

  • Click Edit
  • Enter the following and click Submit
FieldForm TabValueDescription
ConnectionMainElastic Path Commerce Cloud Connection
Webservice PathMainGet value from: Literal
/oauth/access_token
MethodMainPOST
HeadersMainHeader: Content-Type
Value (Literal): application/x-www-form-urlencoded
BodyMainValue (Literal): client_id={{clientId}}&client_secret={{clientSecret}}&grant_type=client_credentials
CachedCachingChecked
Cache Time-to-liveCaching3600
Context Field EnrichmentsUpdate ContextContext Field: clientCredentialsToken
Expression: 'Bearer' + response.access_token

Create a Component to add custom discount to cart

  • Navigate to the Experience Components page (Manage Experiences --> Experience Components)
  • Click the Create Experience Component button
  • Enter the following and click Submit:
FieldValue
Component Codeepcc-add-custom-discount-to-cart
Component NameEPCC Add Custom Discount to Cart
No RulesChecked
Component TypeConscia - Universal API Connector

The DX Engine will create and prepare the Component for further configuration. You should see the new component appear in the Component listing.

  • Click Edit
  • Enter the following and click Submit
FieldForm TabValueDescription
ConnectionMainElastic Path Commerce Cloud Connection
Webservice PathMainGet value from: JS Expression
/v2/carts/${contextField('cartId')}/custom-discounts
MethodMainPOST
HeadersMainHeader: Authorization
Value (Context Field): clientCredentialsToken
HeadersMainHeader: Content-Type
Value (Literal): application/json
BodyMainGet value from: Component Response
Mapper - Discounts
Trigger ExpressionConditionscomponentStatus('mock-validate-discount') === "VALID"
Execution based on dependenciesConditionsSkip On Failed Dependency: Checked
Skip On Skipped Dependency: Checked
Skip On Invalid Dependency: Checked

References

Elastic Path Commerce Cloud API documentation