Skip to main content

Update Inventory in Commerce Engine

You can implement an orchestration flow in the DX Engine based on the following recipe that explains how to update the inventory quantity associated with a product variant in Commercetools when inventory maybe managed in an external system such as an ERP or even a Real-time Inventory Management platform like Fluent Commerce.

Overall Orchestration Flow

The following diagram describes the logical flow involved in this recipe.

alt_text

To set the inventory quantity associated with the master product variant or another product variant in Commercetools, the product must have an SKU (Stock Keeping Unit) identifier.

As opposed to multiple inventory quantities as may occur in organizations that have multiple warehousing locations, this recipe assumes a single inventory quantity. Additionally, this recipe assumes that quantity on stock and available quantity in Commercetools always have the same value.

To set the inventory quantity associated with an SKU in Commercetools, logic must first check for the existence of an inventory entry for that SKU. If such an entry exists, then logic must use the identifier and version of that entry to update the inventory quantity for that SKU. If no entry exists for that SKU, then logic must create that inventory entry.

Mapping Out DX Engine Components and Templates

The following image captured from Conscia’s flow visualizer describes the orchestration process implemented in this recipe.

alt_text

This flow receives an SKU and the intended inventory quantity from context.

To set the inventory quantity for a product variant, the first component in this flow (Commercetools Get Inventory Entry) invokes the Commercetools inventory API to retrieve the existing inventory entry for the given SKU.

If an inventory entry for that SKU exists and the inventory quantity passed to Conscia as context differs from the quantity that inventory entry from Commercetools, then the Commercetools Update Inventory Entry component updates the inventory quantity in that entry, and processing completes.

If that inventory entry for that SKU does not exist, then the Commercetools Create Inventory Entry* component calls the Commercetools inventory API to create that inventory entry, and processing completes.

The Commercetools Set Inventory Quantity template encapsulates the entire flow by specifying the Commercetools Create Inventory Entry and *Commercetools Update Inventory Entry components. Each of these components defines a trigger condition that depends on the Commercetools Get Inventory Entry component.

DX Engine Configuration Details

All the components in this flow use Conscia’s Universal API Connector. Because non-technical users do not interact with the elements involved in this flow and do not need to provide any additional input, none of these components require Experience Rules to be set by business users. To query for inventory entries for the given SKU, the Commercetools Get Inventory Entry component, which retrieves the inventory entry for a product if such an entry exists, GETs the /inventory URL path a the relevant Commercetools API Endpoint using a query string parameter named where that specifies the literal sku%3D%3Asku and the query string parameter named var.sku that retrieves the value of the SKU from the context passed to Conscia.

`${contextField('sku')}`

The Commercetools Create Inventory Entry component, which creates an inventory entry for an SKU if no such entry exists, POSTs to the /inventory URL path at the relevant Commercetools API endpoint. This component uses a JS Expression to define the post body to include the SKU passed in context to Conscia along with two parameters that specify inventory quantities, which this component also retrieves from context.

_.assign({}, {
"sku" : contextField('sku'),
"quantityOnStock" : contextField('quantity'),
"availableQuantity" : contextField('quantity')
})

The Commercetools Create Inventory Entry component specifies a trigger condition that creates a dependency on the Commercetools Get Inventory Entry component, and only runs if the response from that component indicates that there are no inventory entries for the given SKU.

`componentResponse('commercetools-get-inventory-entry').total === 0`

The *Commercetools Update Inventory Entry component, which updates the inventory entry for the product if such an entry exists, uses a JS Expression to define the URL path in Commercetools to which it POSTs. This URL begins with /inventory/, followed by the ID of the inventory entry identified in the response from the Commercetools Get Inventory Entry component.

/inventory/${componentResponse('commercetools-get-inventory-entry').results[0].id}

This component uses a JS Expression to define the post body to include the SKU passed in context to Conscia along with two parameters that specify inventory quantities, which this component also retrieves from context.

_.assign({}, {
"version" : componentResponse('commercetools-get-inventory-entry').results[0].version,
"actions" : [ {
"action" : "changeQuantity",
"quantity" : contextField('quantity')
} ]
})

The Commercetools Update Inventory Entry component specifies a trigger condition that creates a dependency on the Commercetools Get Inventory Entry component, and only runs if the response from that component indicates that there is a single inventory entry for the given SKU and its quantity on stock differs from the value passed in context to Conscia.

`componentResponse('commercetools-get-inventory-entry').total === 1 && componentResponse('commercetools-get-inventory-entry').results[0].quantityOnStock != contextField('quantity')`

References