Skip to main content

Cart

An object with all the items added to the cart by a potential buyer. The frontend can interact with our API to add, update or remove items.

info

The cart is intended to handle only product interacions (addition, removal or update of quantity). Payment methods and shipping are handled on the checkout.

Cart object

PropertyTypeDescription
cart_idStringUnique identifier for the cart.
customer_session_idStringUnique identifier for the customer's session.
shipping_countryObjectInformation about the shipping country (or null if not set).
line_itemsArrayList of items in the cart.
total_amountNumberTotal amount for the items in the cart.
currencyStringCurrency code (e.g., "NOK").
available_shipping_countriesArrayList of countries available for shipping.

Create cart

You can initiate the cart even before adding products to it, so you decide when it's more convenient for you to do it. You need to provide us though with the reference to the customer's session id that is created by you in the frontend.

Retrieve an array of the products added to your channel.

async function createCart() => {
const sdk = getSDK();
let newCart = await sdk.cart.create({
currency: string, // "EUR", "USD", "NOK", "DDK", "SEK", "CHF"
customer_session_id: string, // We recommend an alphanumeric id
});
};
info

Heads up! A cart requirement will be to add a shipping country to it (the country the products are going to be shipped to). Once a product is added to the cart, the paramenter available_shipping_countries will automatically get the shipping countries from the supplier's avaibale shipping countries and return an array with the shipping codes.

You may want to interact with a created cart line items in the following ways:

  • add an additional item o multiple items to the cart object
  • update the quantity of an added item
  • remove an item or multiple items from the cart object

Add an item

From the SDKs product operations, you get all necessary information to complete the line_items object and add an item to the cart.

info

It is possible to add more than one item on this endpoint, just add additional objects to the line_items array.

async function addToCart() => {
const sdk = getSDK();
await sdk.cart.addItem({
cart_id: string,
line_items: [
{
product_id: number,
quantity: number,
variant_id: string,
},
],
});
};
info

Note that since you have added a product to the cart, the parameter available_shipping_countries is updated. This information is usefull when updating the cart shipping country, and once you do it, the product_available_shippings inside each line item will return an array with all the shipping options for that product.

You should also pay attention to the shipping parameter inside each line item. This is null now, but when you assign a shipping to the product, this parameter will return an object with the assigned shipping data.

Update cart shipping country

Once you have created the cart, you can update the shipping country. If you know that you will be working just with one specific country, feel free to update it even before adding a product to the cart.

If you will be working with products that have different shipping countries, you can get the info of the cart available shipping countries directly from the cart object paramenter: available_shipping_countries

  const updateShippingCountry = async (cartId, shippingCountry) => {
const response = await getSDK().cart.update({
cart_id: cartId,
shipping_country: shippingCountry, // examples: "NO", "SE", "DK", "US"
});
return response;
};

Now that you have added a shipping country to the cart, you may see that the line items come with the product available shippings just for that country. This allows you to easily select the shipping id to assign to each product.

Update an item's shipping class or quantity

At any point before initiating the payment you can update the quantity of a line item or the shipping class assigned to it.

It is mandatory to assign a shipping class to a product, a payment cannot be initiated if the shipping class of a line item is null. You can easily do it if the cart has a shipping country as you have the the shipping ids information on the product_available_shippings parameter available in the cart items of the cart.

async function updateCartItem(CART_ID, CART_ITEM_ID, SHIPPING_ID, QUANTITY) => {
const sdk = getSDK();
const response = await sdk.cart.updateItem({
cart_id: CART_ID,
cart_item_id: CART_ITEM_ID,
quantity: QUANTITY // this value is optional
shipping_id: SHIPPING_ID, // this value is optional
});
};

Remove an item

async (CART_ID, CART_ITEM_ID) => {
const sdk = getSDK();
await sdk.cart.deleteItem({
cart_id: CART_ID,
cart_item_id: CART_ITEM_ID
});
};

As response you will get the updated cart. If there was only one product on the cart, the line_items parameter will return an empty array.

Get cart information

At any time you can retrieve cart information with all the added items.

async getCart(CART_ID) => {
const sdk = getSDK();
const cart = await sdk.cart.getById({ cart_id: CART_ID });
return cart;
};

Delete cart

In case you need to delete a cart, it is also possible via the SDK.

async function deleteCart(CART_ID) => {
const sdk = getSDK();
await sdk.shopCart.cart.delete(CART_ID);
};