Skip to main content

Cart API

The Cart API provides comprehensive access to POS cart management functionality, enabling extensions to read cart state, modify line items, apply discounts, manage customer information, and handle cart properties. The API supports both individual and bulk operations for efficient cart manipulation, with selling plan functionality and error handling.

Use cases

  • Real-time monitoring: Monitor cart changes to update extension UI and respond to modifications.
  • Discounts: Apply custom discounts at cart and line item levels, including percentage-based and codes.
  • Product management: Add products programmatically with oversell protection and error handling.
  • Selling plans: Manage selling plans and subscription products with comprehensive operations.

The CartApi object provides access to cart management functionality and real-time cart state monitoring. Access these properties through shopify.cart to interact with the current POS cart.

(address: ) => Promise<void>
required

Add a new address to the customer associated with the cart. The customer must be present in the cart before adding addresses with enhanced address validation and formatting.

(code: string) => Promise<void>
required

Apply a discount code to the cart. The system will validate the code and apply the appropriate discount if the code is valid and applicable to the current cart contents with improved error messaging.

(properties: Record<string, string>) => Promise<void>
required

Add custom key-value properties to the cart for storing metadata, tracking information, or integration data. Properties are merged with existing cart properties with enhanced validation and conflict resolution.

(customSale: ) => Promise<string>
required

Add a custom sale item to the cart with specified quantity, title, price, and taxable status. Returns the UUID of the created line item for future operations and property management.

(variantId: number, quantity: number) => Promise<string>
required

Add a product variant to the cart by its numeric ID with the specified quantity. Returns the UUID of the newly added line item, or an empty string if the user dismissed an oversell guard modal. Throws an error if POS fails to add the line item due to validation or system errors.

Anchor to addLineItemProperties
addLineItemProperties
(uuid: string, properties: Record<string, string>) => Promise<void>
required

Add custom properties to a specific line item using its UUID. Properties are merged with existing line item properties for metadata storage and tracking with enhanced validation.

Anchor to addLineItemSellingPlan
addLineItemSellingPlan
(input: ) => Promise<void>
required

Add a selling plan to a line item in the cart using the line item UUID, selling plan ID, and selling plan name. Optionally provide delivery interval and interval count for improved performance, otherwise POS will fetch them after syncing the cart.

(type: , title: string, amount?: string) => Promise<void>
required

Apply a cart-level discount with the specified type ('Percentage', 'FixedAmount', or 'Code'), title, and optional amount. For discount codes, omit the amount parameter. Enhanced validation ensures proper discount application.

Anchor to bulkAddLineItemProperties
bulkAddLineItemProperties
(lineItemProperties: []) => Promise<void>
required

Add properties to multiple line items simultaneously using an array of inputs containing line item UUIDs and their respective properties for efficient bulk operations with enhanced validation and error reporting.

(cartState: ) => Promise<>
required

Perform a bulk update of the entire cart state including note, discounts, customer, line items, and properties. Returns the updated cart object after the operation completes with enhanced validation and error handling.

Anchor to bulkSetLineItemDiscounts
bulkSetLineItemDiscounts
(lineItemDiscounts: []) => Promise<void>
required

Apply discounts to multiple line items simultaneously. Each input specifies the line item UUID and discount details for efficient bulk discount operations with enhanced validation and allocation tracking.

() => Promise<void>
required

Remove all line items and reset the cart to an empty state. This action can't be undone and will clear all cart contents including line items, discounts, properties, and selling plans.

<>
required

Provides read-only access to the current cart state and allows subscribing to cart changes. The value property provides the current cart state, and subscribe allows listening to changes with improved performance and memory management.

(addressId: number) => Promise<void>
required

Delete an existing address from the customer using the address ID. The customer must be present in the cart to perform this operation with improved error handling for invalid address IDs.

(disableAutomaticDiscounts: boolean) => Promise<void>
required

Remove all discounts from both the cart and individual line items. Set disableAutomaticDiscounts to true to prevent automatic discounts from being reapplied after removal with enhanced discount allocation handling.

() => Promise<void>
required

Remove the current cart-level discount. This only affects cart-level discounts and does not impact line item discounts or automatic discount eligibility.

Anchor to removeCartProperties
removeCartProperties
(keys: string[]) => Promise<void>
required

Remove specific cart properties by their keys. Only the specified property keys will be removed while other properties remain intact with improved error handling for non-existent keys.

() => Promise<void>
required

Remove the currently associated customer from the cart, converting it back to a guest cart without customer-specific benefits or information while preserving cart contents.

(uuid: string) => Promise<void>
required

Remove a specific line item from the cart using its UUID. The line item will be completely removed from the cart along with any associated discounts, properties, or selling plans.

Anchor to removeLineItemDiscount
removeLineItemDiscount
(uuid: string) => Promise<void>
required

Remove all discounts from a specific line item identified by its UUID. This will clear any custom discounts applied to the line item while preserving discount allocation history.

Anchor to removeLineItemProperties
removeLineItemProperties
(uuid: string, keys: string[]) => Promise<void>
required

Remove specific properties from a line item by UUID and property keys. Only the specified keys will be removed while other properties remain intact with improved error handling.

Anchor to removeLineItemSellingPlan
removeLineItemSellingPlan
(uuid: string) => Promise<void>
required

Remove the selling plan from a line item in the cart using the line item UUID. This will clear any subscription or recurring purchase configuration from the line item.

(staffId: number) => Promise<void>
required

Set the attributed staff member for all line items in the cart using the staff ID. Pass undefined to clear staff attribution from all line items with enhanced staff validation and tracking.

Anchor to setAttributedStaffToLineItem
setAttributedStaffToLineItem
(staffId: number, lineItemUuid: string) => Promise<void>
required

Set the attributed staff member for a specific line item using the staff ID and line item UUID. Pass undefined as staffId to clear attribution from the line item with improved validation and error handling.

(customer: ) => Promise<void>
required

Associate a customer with the current cart using the customer object containing the customer ID. This enables customer-specific pricing, discounts, and checkout features with enhanced customer data validation.

(uuid: string, type: , title: string, amount: string) => Promise<void>
required

Apply a discount to a specific line item using its UUID. Specify the discount type ('Percentage' or 'FixedAmount'), title, and amount value with improved discount allocation tracking.

Anchor to updateDefaultAddress
updateDefaultAddress
(addressId: number) => Promise<void>
required

Set a specific address as the default address for the customer using the address ID. The customer must be present in the cart to update the default address with enhanced validation.

Examples

jsx

import {render} from 'preact';

export default async () => {
render(<Extension />, document.body);
};

const Extension = () => {
return (
<s-tile
heading="My App"
subheading="Call cart function"
onClick={() => {
shopify.cart.addCustomSale({
taxable: true,
quantity: 1,
title: 'T-shirt',
price: '10.00',
});
}}
/>
);
};

  • Handle cart state reactively: Use the signal-based interface to automatically update your extension UI when cart changes occur.
  • Validate operations before execution: Check cart editability and validate input data before performing cart operations to prevent errors.
  • Use bulk operations for efficiency: When performing multiple related operations, use bulk methods like bulkCartUpdate, bulkSetLineItemDiscounts, and bulkAddLineItemProperties for better performance and reduced API calls.
  • Handle errors gracefully: Implement proper error handling for all cart operations, as they may fail due to inventory constraints, validation errors, oversell protection, or business rule violations.
  • Manage selling plans appropriately: When working with subscription products, validate selling plan compatibility and handle selling plan requirements.

Cart operations may fail due to business rules, inventory constraints, oversell protection, or validation errors—always implement appropriate error handling.

Was this page helpful?