Issue
- Is there a way to add Bulk products to the cart using the Liferay API?
Environment
- Liferay DXP 7.4 [all versions]
- Quarterly Release
- Commerce 4.0
Resolution
- Liferay provides an API, from which it will help to add bulk products to the cart.
Liferay API: /headless-commerce-delivery-cart/v1.0/carts/{cartId}
- Refer to the below example:
- Navigate to Control Panel > Sites > Create a 'Minium' site.
- Navigate to Control Panel> Accounts> and create an ‘Account', let's say 'Test Account’.
- Hit the ‘
localhost:8080/o/api' Url, open the '/headless-commerce-delivery-cart/v1.0/carts/{cartId}’ API and use the below attributes to add single/bulk products to the cart.
{
"cartItems": [
{
"productId": 35265,
"quantity": 10,
"skuId": 35269
},
{
"productId": 35286,
"quantity": 5,
"skuId": 35302
},
{
"productId": 35326,
"quantity": 35,
"skuId": 35330
},
{
"productId": 35345,
"quantity": 9,
"skuId": 35349
},
{
"productId": 35370,
"quantity": 9,
"skuId": 35374
}
]
} - In the 'skuId' provide the ‘CPInstanceId’, and click on the “Execute” button, and all products will be added to the “Cart“.
Note:
- CartID = OrderID
- productId: Navigated to Control Panel> Commerce> Product, and open any product, let's say ‘ABS Sensor' where we found ‘ProductId’ as ‘ID’
- quantity: We can set the quantity value as per our requirement.
-
skuId: The 'skuId', is ‘CPInstanceId’, which can find in the ‘CPInstance’ table in the Database. (Ex: To find the 'skuId' of 'CPInstanceId' for this product ‘35265’ ID, execute this query in the Database “
SELECT * FROM demo.CPInstance where CPDefinitionId=35265;“.)
- Kindly refer to the attached snapshots for the same.
Additional Information
- Add products to the cart with attributes such as (quantity, size, and color): Please take a look at the JSON format below.
{
"cartItems": [
{
"options": "[
{
"key": "package-quantity",
"skuOptionKey": "package-quantity",
"skuOptionName": "Package Quantity",
"skuOptionValueKey": "112",
"skuOptionValueNames": ["112"],
"value": "112"
},
{
"key": "color",
"skuOptionKey": "color",
"skuOptionName": "Color",
"skuOptionValueKey": "white",
"skuOptionValueNames":["White"],
"value": "white"
},
{
"key": "size",
"skuOptionKey": "size",
"skuOptionName": "Size",
"skuOptionValueKey": "l",
"skuOptionValueNames":["L"],
"value":"l"
}
]",
"quantity": 1,
"replacedSkuId": 0,
"skuId": 35617
}
]
}
Note: Only JSON format has changed, to add bulk product steps, kindly refer to the same steps mentioned in the 'Resolution' section.
Snapshots: