Skip to main content

shopify:cart:attributes-update

Fires when a buyer or an app sets or clears cart attributes. Cart attributes are key-value pairs on the cart, separate from attributes on cart lines, used for delivery dates and gift options.

You can use this to keep your own copy of the attributes in sync, or to react when an attribute your app owns changes.

attributes is the complete set the cart should end up with, replacing the existing set rather than merging with it, so send every key the cart should keep. Leaving out an existing key removes it, and an empty array clears all attributes.

You dispatch it from the element holding the attribute fields, in the cart drawer or on the cart page. Because the event fires when the operation starts, it fires whether or not the change succeeds. If the cart declines the change, the promise resolves with the cart as it stands and userErrors explaining why. If the request fails, the promise rejects and shopify:cart:error is dispatched.

The resolved cart doesn't include attributes. When the promise resolves without userErrors, the event's attributes is the set the cart saved, so you can apply it to your UI when you dispatch, and revert it if the promise rejects or resolves with userErrors.

Anchor to context
context
'product' | 'cart' | 'dialog' | 'standard-action'
required

Where the update came from. The standard-action value is reserved for events that standard storefront actions emit automatically.

Anchor to attributes
attributes
[]
required

The complete set of cart attributes the cart should end up with. This replaces the existing attributes rather than merging with them, so leaving out an existing key removes it and an empty array clears them all.

Anchor to promise
promise
Promise<>
required

Resolves with a CartAttributesUpdateResult when the update finishes. It rejects if the request fails or is aborted.

Anchor to detail
detail
Record<string, unknown>

Optional custom data for the storefront's internal use. Listeners can read it.

Examples

Dispatch the event

import { CartAttributesUpdateEvent } from '@shopify/standard-events';

const attributes = [{ key: 'delivery_date', value: deliveryDateInput.value }];
const deferred = CartAttributesUpdateEvent.createPromise();

element.dispatchEvent(
new CartAttributesUpdateEvent({
context: 'cart',
attributes,
promise: deferred.promise,
}),
);

const ajaxCart = await saveAttributes(attributes);

deferred.resolve({
cart: CartAttributesUpdateEvent.createCartFromAjaxResponse(ajaxCart),
});

Was this page helpful?