Adding products to cart
How to add new items to your shopping cart
Last updated
How to add new items to your shopping cart
Last updated
You want to implement the "add to cart" function on a product page. You have the order ID and either the new item ID or SKU code.
Adding a product (SKU) to a shopping cart means creating a new line item for an order. To do that, send a POST
request to the /api/line_items
endpoint, specifying the order and the SKU relationships.
The following request adds the SKU identified by the "xYZkjABcde" ID to the order identified by the "yzkWXfgHQS" ID:
The following request adds the SKU identified by the "TSHIRTB5B5B5XL" code to the order identified by the "yzkWXfgHQS" ID:
_update_quantity
paramSpecifying "_update_quantity": true
(or "_update_quantity": 1
) in the request body lets you update the existing line item quantity (if any) instead of creating a new line item for the same SKU. That means:
if the item is already present in the shopping cart, its line item quantity attribute is updated
if the item is not present in the shopping cart a new line item is created and its quantity attribute is set to the quantity
value
If you send the POST request without the "_update_quantity": true
(or "_update_quantity": 1
) param a new line item is always created, even if the line item SKU is already present in the shopping cart.
name
and image_url
fieldsWhen creating a new line item, the only required attribute is quantity
. Anyway, we recommend always populating the name
and image_url
fields (like in the examples above) in order to show the desired name and image in the cart summary and order management. When empty, Commerce Layer will try to populate name
and image_url
from the associated SKU fields.
See our documentation if you need more information on how to create a line item.