Skip to main content

Cart

Note

This is a legacy API. Use the latest version of POS instead.


These actions require the following app versions:

  • Point of Sale iOS v5.11.0 or above
  • Point of Sale Android v3.3.2 or above
Note

Cart features are only available when opening an embedded app through a link on Point of Sale while editing the cart. To learn more about how to check for cart features, see Features.


Create an app and import the Cart module from @shopify/app-bridge/actions. Note that we'll be referring to this sample application throughout the examples below.

Note

In the following example, config is a valid App Bridge configuration object. Learn more about configuring App Bridge.

import createApp from '@shopify/app-bridge';
import { Cart } from '@shopify/app-bridge/actions';

const app = createApp(config);

Create a cart and subscribe to cart updates:

var cart = Cart.create(app);
cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] cart update', payload);
});

app.error(function (data: Error.ErrorAction) {
console.info('[client] Error received: ', data);
});

Cart actions are available only on the Shopify Point of Sale app, so it's a good idea to check if an action is available before you call it. This makes sure that you're able to display the appropriate UI when a feature is or is not available. To learn more about Feature Detection, see Features.

The following example shows how you could use Feature Detection with cart actions by requesting if Cart.Action.FETCH is available and using the result to alter your UI.

Start off by requesting if the cart group is available:

app.featuresAvailable([Group.Cart]).then(function (state) {...});

The promise block resolves to a state object containing the status of the cart actions. Query for Cart.Action.FETCH and then Dispatch inside that object. If it's true, follow the instructions below for Fetch cart. If it's false, the cart action is not available in this context. Using this approach, it's possible to distinguish between an empty cart and a context where cart is not available.

app.featuresAvailable(Group.Cart).then(function (state) {
var _ref = state.Cart && state.Cart[Cart.Action.FETCH],
Dispatch = _ref.Dispatch;

if (Dispatch) {
cart.dispatch(Cart.Action.FETCH);
} else {
var toastOptions = {
message: 'Cart is not available',
duration: 5000,
isError: true
};
var toastError = Toast.create(app, toastOptions);
toastError.dispatch(Toast.Action.SHOW);
}
});

GroupCart
ActionUPDATE
Action TypeAPP::CART::UPDATE
DescriptionRetrieves the latest state of the currently active cart from Shopify POS.

Subscribing to this action provides you with cart updates.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] fetchCart', payload);
unsubscriber();
});
// ...
// Call other Cart actions

Anchor to NoteAttribute PayloadNoteAttribute Payload

KeyTypeDescription
nameStringThe name of the attribute.
valueStringThe value of the attribute.

KeyTypeDescription
subtotalStringThe total cost of the current cart including discounts, but before taxes and shipping. Value is based on the shop's existing currency settings.
taxTotalStringThe sum of taxes for the current cart. Value is based on the shop's existing currency settings.
grandTotalStringThe total cost of the current cart, after taxes and discounts have been applied. Value is based on the shop's existing currency settings.
customerCustomer?The customer associated to the current cart.
lineItemsArray[LineItem]A list of lineItem objects.
noteAttributesArray[NoteAttribute]?A list of objects containing cart properties.
cartDiscountDiscount?The current discount applied to the entire cart.
noteString?A note associated with the cart.

GroupCart
ActionFETCH
Action TypeAPP::CART::FETCH
DescriptionRequests the currently active cart from Shopify POS.

A cart needs to call fetch before receiving data in Cart.Action.UPDATE:

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] fetchCart', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.FETCH);

GroupCart
ActionSET_CUSTOMER
Action TypeAPP::CART::SET_CUSTOMER
DescriptionSets a customer on the current cart.

This is the customer that will be attached to the cart and subsequent order. You can either set a customer with an existing customer ID or create a new customer.

Existing customer with ID:

var customerPayload = {
id: 123
};

New customer:

var customerPayload = {
email: 'voisin@gmail.com',
firstName: 'Sandrine',
lastName: 'Voisin',
note: 'First customer of 2019',
};
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] setCustomer', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.SET_CUSTOMER, {
data: customerPayload
});

KeyTypeDescription
dataCustomerThe customer data.

KeyTypeDescription
idNumber?The ID of existing customer.
emailString?The email for new customer.
firstNameString?The first name for new customer.
lastNameString?The last name for new customer.
noteString?The note for new customer.

Anchor to Add customer addressAdd customer address

GroupCart
ActionADD_CUSTOMER_ADDRESS
Action TypeAPP::CART::ADD_CUSTOMER_ADDRESS
DescriptionAdds a new address on the customer associated with the current cart.
Note

Note: A customer must exist on the cart for this action to be successful.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] addCustomerAddress', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.ADD_CUSTOMER_ADDRESS, {
data: {
address1: '528 Old Weston Road',
address2: 'Apartment 201',
city: 'Toronto',
company: 'Eliteweb Inc.',
country: 'Canada',
countryCode: 'CA',
firstName: 'Sandrine',
lastName: 'Voisin',
name: 'Sandrine Voisin',
phone: '416 684 1787',
province: 'Ontario',
provinceCode: 'ON',
zip: 'M6N 3B1',
}
});

KeyTypeDescription
dataAddressThe data for creating a new address.

KeyTypeDescription
address1String?The customer's primary address.
address2String?Any extra information associated with the address (Apartment #, Suite #, Unit #, etc.).
cityString?The name of the customer's city.
companyString?The company name associated with address.
firstNameString?The first name of the customer.
lastNameString?The last name of the customer.
phoneString?The phone number of the customer.
provinceString?The province or state of the address.
countryString?The country of the address.
zipString?The ZIP or postal code of the address.
nameString?The name of the address.
provinceCodeString?The acronym of the province or state.
countryCodeString?The Country Code in ISO 3166-1 (alpha-2) format.

Anchor to Update customer addressUpdate customer address

GroupCart
ActionUPDATE_CUSTOMER_ADDRESS
Action TypeAPP::CART::UPDATE_CUSTOMER_ADDRESS
DescriptionUpdates a new address on the customer associated with the current cart.
Note

Note: The first parameter is the index of the customer address you're updating. If there is no address at that index, this action will not be successful.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] updateCustomerAddress', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.UPDATE_CUSTOMER_ADDRESS, {
index: 0,
data: {
address1: '528 Old Weston Road',
address2: 'Apartment 201',
city: 'Toronto',
company: 'Eliteweb Inc.',
country: 'Canada',
countryCode: 'CA',
firstName: 'Sandrine',
lastName: 'Voisin',
name: 'Sandrine Voisin',
phone: '416 684 1787',
province: 'Ontario',
provinceCode: 'ON',
zip: 'M6N 3B1',
}
});

KeyTypeDescription
indexNumberThe index of the address to update.
dataAddressThe fields of the address to update.

GroupCart
ActionREMOVE_CUSTOMER
Action TypeAPP::CART::REMOVE_CUSTOMER
DescriptionRemoves a customer from the current cart.
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeCustomer', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_CUSTOMER);

GroupCart
ActionSET_DISCOUNT
Action TypeAPP::CART::SET_DISCOUNT
DescriptionSets a discount on the current cart.

You can apply a discount to the entire cart, which will affect all line items. There are several different types of discounts. See below for an example of each type.

Flat amount discount:

var discountPayload = {
amount: 1,
discountDescription: "$1 off discount",
type: 'flat',
}

Percentage discount:

Note

Note: The amount is a float value where 1.0 is a 100% discount and 0.0 is a 0% discount.

var discountPayload = {
amount: 0.5,
discountDescription: "50% off discount",
type: 'percent',
}

Discount code discount:

var discountPayload = {
discountCode: 'HOLIDAYSALE50',
}
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] setDiscount', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.SET_DISCOUNT, {
data: discountPayload
});

Anchor to Discount Amount PayloadDiscount Amount Payload

KeyTypeDescription
amountNumberThe discount amount to be applied to the subtotal of the cart as a flat value or total percentage discount. flat discount amounts must be greater than 0. Discounts greater than the subtotal of the cart will be reduced to the subtotal amount. percent discount amounts must be between 0.0 and 1.0.
discountDescriptionString?A description of the discount being applied. Defaults to Discount, if not supplied.
typeString?The discount type. The options are flat or percent. Defaults to flat, if not supplied.

Anchor to Discount Code PayloadDiscount Code Payload

KeyTypeDescription
discountCodeStringThe discount code to apply to the current cart.

KeyTypeDescription
dataDiscountAmount | DiscountCodeThe type of discount to apply to the current cart.

GroupCart
ActionREMOVE_DISCOUNT
Action TypeAPP::CART::REMOVE_DISCOUNT
DescriptionRemoves a discount from the current cart.
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeDiscount', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_DISCOUNT);

GroupCart
ActionSET_PROPERTIES
Action TypeAPP::CART::SET_PROPERTIES
DescriptionAdds additional properties to the current cart.
Note

Note: Each key/value pair in the payload is a single property.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] setProperties', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.SET_PROPERTIES, {
data: {
referral: 'Shopify',
userID: '1234',
}
});

KeyTypeDescription
dataObjectThe key-value pairs of properties to add to the current cart.

Anchor to Remove cart propertiesRemove cart properties

GroupCart
ActionREMOVE_PROPERTIES
Action TypeAPP::CART::REMOVE_PROPERTIES
DescriptionRemoves one or more properties from the current cart.
Note

Note: When removing properties, reference the key of the property. In the example above, two properties were set on the cart, with keys referral and userID. Pass in either of those values to remove them.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeProperties', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_PROPERTIES, {
data: ['referral', 'userID']
});

KeyTypeDescription
dataArray[String]A list of properties to remove from the current cart.

GroupCart
ActionADD_LINE_ITEM
Action TypeAPP::CART::ADD_LINE_ITEM
DescriptionAdds a new line item to the current cart.

Line items can be added to the cart in two different ways: as a variant of a product, or as a quick sale item (usually used for one-off sales on items not backed by a variant).

Quick Sale line item:

var lineItemPayload = {
price: 20,
quantity: 1,
title: 'Bab Low - Blue Jay // White Soles',
taxable: true
};

Variant line item:

var lineItemPayload = {
variantId: 1234,
quantity: 1
};
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] addLineItem', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.ADD_LINE_ITEM, {
data: lineItemPayload
});

KeyTypeDescription
priceNumber?The price of the line item. Required if a variantId is not provided. Must be greater than or equal to 0.
quantityNumberThe amount of items to add. Defaults to 1 if not provided. Must be greater than 0.
titleString?The name of the product, defaults to "Quick sale" if not supplied.
variantIdNumber?The unique ID of the variant being added. If not included, the product will be considered a custom sale.
discountDiscountAmount?A discount to apply to the line item.
taxableBoolean?A flag that indicates if the line item changes the taxes of the cart. Defaults to true if not provided. Only available to be set on custom sale items.
propertiesArray[NoteAttribute]?A list of objects containing item properties.

KeyTypeDescription
dataLineItemThe data for creating a new line item in the current cart.

GroupCart
ActionUPDATE_LINE_ITEM
Action TypeAPP::CART::UPDATE_LINE_ITEM
DescriptionUpdates an existing line item quantity in the cart.
Note

Note: The first parameter is the index of the line item to update. If there is no line item at that index, this action will not be successful. Only the quantity property can be updated.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] updateLineItem', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.UPDATE_LINE_ITEM, {
index: 0,
data: {
quantity: 100,
}
});

Anchor to Line item update payloadLine item update payload

KeyTypeDescription
quantityNumberThe amount of items to add. Must be greater than 0.

KeyTypeDescription
indexNumberIndex of line item to update
dataLine Item UpdateThe data for updating a line item at the specified index.

GroupCart
ActionREMOVE_LINE_ITEM
Action TypeAPP::CART::REMOVE_LINE_ITEM
DescriptionRemoves an existing line item from the cart.
Note

Note: The first parameter is the index of the line item to remove. If there is no line item at that index, this action will not be successful.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeLineItem', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_LINE_ITEM, {
index: 0
});

KeyTypeDescription
indexNumberThe index of line item to remove.

Anchor to Set line item discountSet line item discount

GroupCart
ActionSET_LINE_ITEM_DISCOUNT
Action TypeAPP::CART::SET_LINE_ITEM_DISCOUNT
DescriptionSets the discount on a line item in the current cart.

Unlike cart discounts, line item discounts can't use discount codes. They support only flat amount discounts and percentage discounts.

Note

Note: The first parameter is the index of the line item to applying the discount to. If there is no line item at that index, this action will not be successful.

Flat amount discount:

var discountPayload = {
amount: 1,
discountDescription: "$1 off discount",
type: 'flat',
}

Percentage discount:

Note

Note: The amount is a float value where 1.0 is a 100% discount and 0.0 is a 0% discount.

var discountPayload = {
amount: 0.5,
discountDescription: "50% off discount",
type: 'percent',
}
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] setLineItemDiscount', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.SET_LINE_ITEM_DISCOUNT, {
index: 0,
data: discountPayload
});

KeyTypeDescription
indexNumberThe index of the line item to apply the discount to.
dataDiscountAmountThe discount to apply to the line item.

Anchor to Remove line item discountRemove line item discount

GroupCart
ActionREMOVE_LINE_ITEM_DISCOUNT
Action TypeAPP::CART::REMOVE_LINE_ITEM_DISCOUNT
DescriptionRemoves a line item discount in the current cart.
Note

Note: The first parameter is the index of which line item we're removing a discount from. If there is no line item at that index then this action will not be successful.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeLineItemDiscount', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_LINE_ITEM_DISCOUNT, {
index: 0
});

KeyTypeDescription
indexNumberThe index of the line item to remove the discount from.

Anchor to Set line item propertiesSet line item properties

GroupCart
ActionSET_LINE_ITEM_PROPERTIES
Action TypeAPP::CART::SET_LINE_ITEM_PROPERTIES
DescriptionAdds a property to a given line item in the current cart.
Note

Note: The first parameter is the index of which line item we're setting properties on. If there is no line item at that index then this action will not be successful.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] setLineItemProperties', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.SET_LINE_ITEM_PROPERTIES, {
index: 0,
data: {
referral: 'Shopify',
userID: '1234',
}
});
Note

Note: Each key/value pair in the payload is a single property.

KeyTypeDescription
dataObjectThe key-value pairs of properties to add to the specified line item.

Anchor to Remove line item propertiesRemove line item properties

GroupCart
ActionREMOVE_LINE_ITEM_PROPERTIES
Action TypeAPP::CART::REMOVE_LINE_ITEM_PROPERTIES
DescriptionRemoves a property from a given line item in the current cart.
Note

Note: The first parameter is the index of which line item we're removing properties from. If there is no line item at that index then this action will not be successful.

When removing properties, reference the key of property. In the properties that were set on the cart above, the keys were: referral and userID. You can pass in either of those values to remove them.

var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] removeLineItemProperties', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.REMOVE_LINE_ITEM_PROPERTIES, {
index: 0,
data: ['referral', 'userID']
});

KeyTypeDescription
indexNumberThe index of the line item to remove properties from.
dataArray[String]A list of properties to remove from the specified line item.

GroupCart
ActionCLEAR
Action TypeAPP::CART::CLEAR
DescriptionRemove all line items from the current cart.
var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
console.log('[Client] clear', payload);
unsubscriber();
});
cart.dispatch(Cart.Action.CLEAR);

Was this page helpful?