Cart
Cart
|
|
Note
This component was introduced in version |
The Cart controller exposes methods for managing the cart in a commerce interface.
Methods
empty
Sets the quantity of each item in the cart to 0, effectively emptying the cart.
Emits an ec.cartAction analytics event with the remove action for each item removed from the cart.
purchase
Emits an ec.purchase analytics event and then empties the cart without emitting any additional events.
Parameters
-
transaction:
TransactionThe object with the id and the total revenue from the transaction, including taxes, shipping, and discounts.
updateItemQuantity
Creates, updates, or deletes an item in the cart, and emits an ec.cartAction analytics event if the quantity of the item in the cart changes.
If an item with the specified productId, name and price already exists in the cart: - Setting quantity to 0 deletes the item from the cart. - Setting quantity to a positive number updates the item.
Otherwise: - Setting quantity to a positive number creates the item in the cart with the specified name, price, and quantity.
If the specified quantity is equivalent to the current quantity of the item in the cart, no analytics event is emitted. Otherwise, the method emits an ec.cartAction event with the appropriate action (add or remove).
Note: Updating the name or price will create a new item in the cart, leaving the previous item with the same productId, name and price unchanged. If you wish to change these items, delete and recreate the item.
Parameters
-
item:
CartItemThe cart item to create, update, or delete.
subscribe
Adds a callback that’s invoked on state change.
Parameters
-
listener:
() => voidA callback that’s invoked on state change.
Returns Unsubscribe: A function to remove the listener.
Attributes
state
A scoped and simplified part of the headless state that is relevant to the Cart controller.
Properties
-
items:
CartItem[]The items in the cart.
-
totalPrice:
numberThe sum total of the
pricemultiplied by thequantityof each item in the cart. -
totalQuantity:
numberThe sum total of the
quantityof each item in the cart.
Initialize
buildCart
Creates a Cart controller instance.
Parameters
-
engine:
CommerceEngineThe headless commerce engine.
-
props:
CartPropsThe configurable
Cartproperties.
Returns Cart
CartProps
The configurable Cart properties.
Properties
-
initialState?:
CartInitialStateThe initial state to apply to this
Cartcontroller.
CartInitialState
The initial state to apply to this Cart controller.
Properties
-
items?:
CartItemWithMetadata[]
Related types
CartItem
Properties
-
name:
stringThe human-readable name of the product.
-
price:
numberThe price per unit of the product.
-
productId:
stringThe unique identifier of the product.
-
quantity:
numberThe number of units of the product in the cart.
CartItemWithMetadata
Properties
-
name:
stringThe name of the cart item.
-
price:
numberThe price of the cart item.
-
productId:
stringThe unique identifier of the product.
-
quantity:
numberThe quantity of the product in the cart.
Transaction
Properties
-
id:
stringThe transaction’s id
-
revenue:
numberThe total revenue from the transaction, including taxes, shipping, and discounts.
Unsubscribe
Call signatures
-
(): void;