Skip to main content

Elastic Path - Add to Cart

In the following recipe, we will walk through an add to cart flow that includes authentication and an inventory check before successfully adding a product to the cart. 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 browsing an online storefront.
  • They add a product to their cart from a Product Detail Page or perhaps a shoppable landing page, or anywhere an Add to Cart button is enabled.
  • This sets off a short chain of events in Conscia.

alt_text

Mapping Out DX Engine Components and Templates

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

alt_text

When the shopper presses the Add to Cart button, the storefront sends off a query request to the DX Engine. It calls the Add Product to Cart component and supplies the productId and quantity requested by the shopper, as well as the item type as required by Elastic Path*. The call looks like this:

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

{
"componentCodes": ["epcc-add-to-cart"],
"context": {
"productId": "ff11915c-4b63-48bd-aafe-83156e2d1206",
"type": "cart_item",
"quantity": 2
}
}

*Note: The item type can also be set on the Orchestration Component itself in Conscia instead of the front end supplying that value.

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

The first three steps happen in parallel, since they are not dependent on each other, but all need to be done before the product can actually be added to the cart. Add Product to Cart will only fire once these three are complete and responses validated.

Retrieve CartId: Elastic Path does not generate cart ids and expects the front end to supply one before creating a cart. A mocked service here is called to provide a cart id, which is then stored in a Context field for future reference.

Check Inventory: At the same time, a mocked service is being called to retrieve stock levels for this product. Elastic Path handles inventory natively, but it is also common for businesses to track inventory externally in other systems, especially when there are multiple warehouses and multiple storefronts involved and the tracking becomes more complex. In these cases you will need to make a call to an OMS or ERP, for example, to check the most up-to-date inventory levels. This component could be enhanced to pass over location information to check specific store inventory for BOPIS or regional warehouse stock as well.

Generate Token: The storefront also needs to generate an implicit authorization token to enable the Add to Cart call to be made. Implicit bearer tokens are good for an hour, so the value will be set in a Context field and cached.

Add Product to Cart: With an authorization token, a cart id, and a successful inventory check, the add to cart call can now go through. The additional logic is on this component to verify that the inventory check went through and that there is enough stock available for the shopper to purchase this product in the requested quantity.

DX Engine Configuration Details

You will need to configure a connection to Elastic Path Commerce Cloud. For the mock calls to external systems for inventory and cartIds, 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 an OMS 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 retrieve the cartId

  • 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-get-cartId
Component NameMock Get CartId
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
Trigger ExpressionConditionstrue
Validity ExpressionConditionstrue
ConnectionMainN/A
Webservice PathMainGet value from: Literal
https://r7jqm.wiremockapi.cloud/cartId
Use any API mocking service of your choice. This call returns a cartId string value.
MethodMainGET
Context Field EnrichmentsUpdate ContextContext Field: cartId

Expression
response

Create a Component to check inventory

  • 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-get-inventory-by-productId
Component NameMock Get Inventory by ProductId
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/${contextField('productId')}
Use any API mocking service of your choice. This call takes a productId in the path and returns an integer value.
MethodMainGET

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 add product 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-product-to-cart
Component NameEPCC Add Product 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')}/items
MethodMainPOST
HeadersMainHeader: Authorization
Value (Context Field): implicitToken
HeadersMainHeader: Content-Type
Value (Literal): application/json
BodyMainGet value from: JS Expression
{"data": {"id": "${contextField('productId')}", "type": "cart_item","quantity": ${contextField('quantity')}}
Trigger ExpressionConditionscomponentStatus('mock-get-inventory-by-productId') === "VALID" && contextField('quantity') <= componentResponse('mock-get-inventory-by-productId')
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