Skip to main content
openbridge.cart.add(variantId, quantity)
openbridge.cart.add(variantId, options)
openbridge.cart.add(lines)
Adds one or more product variants to the cart.

Parameters

Single item (simple)

ParamTypeRequiredDescription
variantIdstringYesProduct variant GID
quantitynumberNoQuantity to add (default: 1)

Single item (with options)

ParamTypeRequiredDescription
variantIdstringYesProduct variant GID
options.quantitynumberNoQuantity to add (default: 1)
options.sellingPlanIdstringNoSelling plan GID for pre-orders/subscriptions

Multiple items

ParamTypeRequiredDescription
linesCartLineInput[]YesArray of line items

Returns

Promise<ShopifyCart>

Examples

Add single item

await openbridge.cart.add('gid://shopify/ProductVariant/123')

Add with quantity

await openbridge.cart.add('gid://shopify/ProductVariant/123', 3)

Add multiple items

await openbridge.cart.add([
    { merchandiseId: 'gid://shopify/ProductVariant/123', quantity: 2 },
    { merchandiseId: 'gid://shopify/ProductVariant/456', quantity: 1 }
])

With product data

// After fetching a product
const product = await openbridge.products.get('awesome-tshirt')
const variant = product.variants[0]

await openbridge.cart.add(variant.id, 1)

Add to cart button

document.querySelector('.add-to-cart').addEventListener('click', async () => {
    const variantId = this.dataset.variantId
    const quantity = parseInt(this.dataset.quantity) || 1

    await openbridge.cart.add(variantId, quantity)
})

Add with selling plan (pre-order)

Use this to add items with a selling plan attached. When the customer checks out, they’ll pay the deposit amount defined in the selling plan.
await openbridge.cart.add('gid://shopify/ProductVariant/123', {
    quantity: 1,
    sellingPlanId: 'gid://shopify/SellingPlan/456'
})

Add multiple items with selling plans

await openbridge.cart.add([
    {
        merchandiseId: 'gid://shopify/ProductVariant/123',
        quantity: 1,
        sellingPlanId: 'gid://shopify/SellingPlan/456'
    },
    {
        merchandiseId: 'gid://shopify/ProductVariant/789',
        quantity: 2
    }
])

Check selling plan on cart line

const lines = openbridge.cart.lines()

lines.forEach(line => {
    if (line.sellingPlanAllocation) {
        console.log('Plan:', line.sellingPlanAllocation.sellingPlan.name)
        console.log('Deposit:', line.sellingPlanAllocation.checkoutChargeAmount.amount)
        console.log('Remaining:', line.sellingPlanAllocation.remainingBalanceChargeAmount.amount)
    }
})