Skip to content

Cart Items

Overview

The items part of the cart is used to manage the items in the cart. You can add, update, remove, and delete items in the cart. Everything you need to make a shopping cart work.

Types

The following types for managing cart items:

@geins/types
typescript
type CartItemType = {
    id?: string;
    groupKey?: string;
    type?: ItemType;
    title?: string;
    product?: ProductType;
    skuId?: number;
    quantity: number;
    campaign?: CampaignType;
    productPackage?: ProductPackageCartItemType;
    productPackageCartItems?: CartItemType[];
    message?: string;
    totalPrice?: PriceType;
    unitPrice?: PriceType;
};

declare enum ItemType {
    PRODUCT = "PRODUCT",
    PACKAGE = "PACKAGE"
}

type ProductPackageSelectionType = {
    groupId: number;
    optionId: number;
    skuId: number;
};

type ProductPackageCartItemType = {
    packageId: number;
    packageName: string;
    groupId: number;
    optionId: number;
};

Identifying Items

Each item in the cart has a unique id that is generated by the system. This id can be used to identify the item when updating or removing it from the cart.

Getting Items

The get method is used to get all items in the cart.

Example:

typescript
// get all items in the cart
const items = await geinsOMS.cart.items.get();

Read the JSdoc for more details on the get method.

Adding Items

Adding an item to the cart is done using the add method. A Cart will be created if there isnt one already. In its simplest form, you can add an item by providing the skuId of the product you want to add to the cart.

Example:

typescript
// add item with id 1234 to cart
await geinsOMS.cart.items.add({ skuId: 1234 });

See JSdoc for more details on the add method.

Product

There is a few features that will help you with some common use cases when adding items to the cart. For example, you can add a product with a specific quantity, or you can add a product with a message.

Example:

typescript

// add item with id 1234 to cart with quantity 2
const argsQuantity = { skuId: 1234, quantity: 2 };
await geinsOMS.cart.items.add(argsQuantity);

// add item with id 1234 to cart with a message 
const argsMessage = { skuId: 1234, message: "This is a message" };
await geinsOMS.cart.items.add(argsMessage);

// add item with id 1234 to cart with a message 
const argsMessage1 = { skuId: 1234, message: "This is a message" };
await geinsOMS.cart.items.add(argsMessage);

// add item with id 1234 to cart with another message 
const argsMessag2 = { skuId: 1234, message: "This is a message two" };
await geinsOMS.cart.items.add(argsMessage2);

The Id of the item will be the same aslong as the skuId is the same and the message is the same.

Package / Bundle

Adding a product package to the cart is similar to adding a product. You can add a product package with a specific quantity, or you can add a product package with a message.

A product package is a group of products that are sold together. Each product package has a unique packageId and can have multiple selections. Each selection is a combination of a groupId, optionId, and skuId.

Example:

typescript

let args = {
  packageId: 1234,
  selections: [
    { groupId: 1, optionId: 19, skuId: 1329 },
    { groupId: 2, optionId: 2, skuId: 1325 },
    { groupId: 3, optionId: 4, skuId: 1342 },
    { groupId: 4, optionId: 9, skuId: 1334 },
    { groupId: 5, optionId: 12, skuId: 1338 }
  ]
};

try {
  await geinsOMS.cart.items.add(argsPackage);
} catch(e){
    // handle error
}

⚠️ Warning

When adding a product package to the cart the add method will throw an error if the selections are not valid. So make sure to error handle the response.

Updating Items

The update method is used to update an item in the cart. You can update the quantity of an item, or you can update the message of an item.

Example:

typescript
// update the quantity of the item with id 1234 to 3
await geinsOMS.cart.items.update({ id: "1234", quantity: 3 });

// update the message of the item with id 1234
await geinsOMS.cart.items.update({ id: "1234", message: "This is a new message" });

Read the JSdoc for more details on the update method.

Removing Items

The remove method is used to remove units item from the cart. Items with quantity < 1 will be removed from the cart.

Example:

typescript
// removes one unit of the item with id 1234
await geinsOMS.cart.items.remove({ id: "1234", quantity: 1 });

If you want to remove all units of an item, you can set the quantity to the total quantity of the item or use the delete method.

Read the JSdoc for more details on the remove method.

Deleting Items

The delete method is used to remove an item from the cart.

Example:

typescript
// remove the item id 1234 from the cart
await geinsOMS.cart.items.delete({ id: "1234" });

Read the JSdoc for more details on the delete method.

Clearing all items

If you want to remove all items from the cart, you can use the clear method.

Example:

typescript
// clear all items from the cart
const result = await geinsOMS.cart.items.clear();
if(result === true){
    console.log("Cart is now empty"); 
}

Read the JSdoc for more details on the clear method.

Released under the MIT License.