Basics: Cart
Introduction
This section will cover the basics of the cart and how to use it. We will also cover how to add, remove and clear products from the cart.
API Reference
You can find all available BigCommerce module methods and their description in the API Reference.
Creating a cart
To create a new cart, you can use the createCart method.
The channel id
and if authenticated, the customers id
will be automatically picked up from the cookie in the requests header.
import { sdk } from '~/sdk.config.ts';
const receivedResponse = await sdk.bigcommerce.createCart({
data: {
line_items: []
}
});
It is possible to create a cart with items added to it by passing the lineItems
property with the relevant product data and quantity.
import { sdk } from '~/sdk.config.ts';
const cart = await sdk.bigcommerce.createCart({
data: {
lineItems: [{
quantity: 2,
productId: 230,
optionSelections: [{
optionId: 10,
optionValue: "Some Text Value"
}]
}]
}
});
Fetching the cart
To fetch the cart, you can use the getCart method.
Fetching a cart by its id is the most common use case. To fetch a cart, you can pass the cart's id as the id
parameter.
import { sdk } from '~/sdk.config.ts';
const cart = await sdk.bigcommerce.getCart({ id: "930a1424-e8e8-4757-b4f6-8de76c8106cb" });
Deleting a cart
To delete a cart, you can use the deleteCart method passing the cart id
.
import { sdk } from '~/sdk.config.ts';
await sdk.bigcommerce.deleteCart({ id: "930a1424-e8e8-4757-b4f6-8de76c8106cb" })
Add items to cart
You can use the addCartItems method to add the items to the cart by specifiying the id
of the product and the quantity
.
import { sdk } from '~/sdk.config.ts';
const addCartItem = await sdk.bigcommerce.addCartItems({
data: {
cartId: '7c3ab631-782c-4747-8ccc-873e87de5152',
line_items: [{
product_id: 112,
quantity: 1
}]
}
})
When adding a product with variants, a variant_id
should be passed together with the line_items
const receivedResponse = await sdk.bigcommerce.addCartItems({
cartId: '7c3ab631-782c-4747-8ccc-873e87de5152'
data: {
line_items: [{
product_id: 115,
quantity: 1,
variant_id: 91
}]
}
});
Update Cart Items
You can use the updateCartItems method to update the items to the cart by specifiying the id
of the cart, the cart item id
(please note that cart item id is not the same as the product's id), and the updated product data.
import { sdk } from '~/sdk.config.ts';
const updateCartItem = await sdk.bigcommerce.updateCartItem({
cartId: '7c3ab631-782c-4747-8ccc-873e87de5152',
itemId: 'bd64d491-a4ac-4839-9fc2-5fc9f95a3bfd'
data: {
line_items:[{
quantity: 2,
product_id: 77
}]
}
})
Remove Cart Item
You can use the removeCartItem method to remove the item from the cart by specifiying the id
of the cart and the cart item id
(please note that cart item id is not the same as the product's id).
import { sdk } from '~/sdk.config.ts';
const removeCartItemResponse = await sdk.bigcommerce.removeCartItem({
data: {
cartId: '7c3ab631-782c-4747-8ccc-873e87de5152',
itemId: 'bd64d491-a4ac-4839-9fc2-5fc9f95a3bfd'
}
})