create Cart Handlerutility
Creates an API that can be used to interact with the cart.
Anchor to createcarthandler(options)createCartHandler(options)
A function that returns the cart id in the form of .
A function that sets the cart ID.
The cart mutation fragment used in most mutation requests, except for and
. See the example usage in the documentation.
The cart query fragment used by cart.get()
. See the example usage in the documentation.
Define custom methods or override existing methods for your cart API instance. See the example usage in the documentation.
Anchor to returnsReturns
The handler returns the following default methods. Any custom or overwritten methods will also be available in the returned cart instance.
Adds a delivery address to the cart.
Adds items to the cart. If the cart doesn't exist, a new one will be created.
Creates a new cart.
Removes a custom field (metafield) from the cart.
Retrieves the cart information.
Retrieves the unique identifier of the cart. By default, it gets the ID from the request cookie.
Removes a delivery address from the cart
Removes items from the cart.
Sets the unique identifier of the cart. By default, it sets the ID in the header cookie.
Adds extra information (metafields) to the cart. If the cart doesn't exist, a new one will be created.
Updates additional information (attributes) in the cart.
Updates the buyer's information in the cart. If the cart doesn't exist, a new one will be created.
Update cart delivery addresses.
Updates discount codes in the cart.
Updates gift card codes in the cart.
Updates items in the cart.
Updates the note in the cart. If the cart doesn't exist, a new one will be created.
Updates the selected delivery options in the cart. Only available for carts associated with a customer access token.
Server.(js|ts)
Anchor to examplesExamples
Examples of various ways to use the utility.
Anchor to example-cart-fragmentsCart fragments
Anchor to example-exampleExample
Use and
to change the cart data the queries will return.
Example
JavaScript
Anchor to example-custom-methodsCustom methods
Anchor to example-exampleExample
Define or override methods in your cart handler instance. Note that for addLines, updateDiscountCodes, updateBuyerIdentity, updateNote, updateAttributes, and setMetafields, if you override any of these methods, a new cart will not be created unless you implement the cart creation logic in your overriding method.
Example
JavaScript
Anchor to example-cart-instance-usageCart instance usage
Anchor to example-cart.addlinesCart.addLines
Add items to the cart. If the cart does not exist, a new cart will be created.
Anchor to example-cart.createCart.create
Create a new cart.
Anchor to example-cart.deletemetafieldCart.deleteMetafield
Delete extra information (metafield) from the cart.
Anchor to example-cart.getCart.get
Retrieve the cart information.
Anchor to example-cart.getcartidCart.getCartId
Get the unique identifier of the cart.
Anchor to example-cart.removelinesCart.removeLines
Remove items from the cart.
Anchor to example-cart.setcartidCart.setCartId
Set the unique identifier of the cart.
Anchor to example-cart.setmetafieldsCart.setMetafields
Add extra information (metafields) to the cart. If the cart does not exist, a new cart will be created.
Anchor to example-cart.updateattributesCart.updateAttributes
Update additional information (attributes) in the cart. If the cart does not exist, a new cart will be created.
Anchor to example-cart.updatebuyeridentityCart.updateBuyerIdentity
Update the buyer’s information in the cart. If the cart does not exist, a new cart will be created.
Anchor to example-cart.updatediscountcodesCart.updateDiscountCodes
Update discount codes in the cart.
Anchor to example-cart.updategiftcardcodesCart.updateGiftCardCodes
Update gift card codes in the cart.
Anchor to example-cart.updatelinesCart.updateLines
Update items in the cart.
Anchor to example-cart.updatenoteCart.updateNote
Update the note in the cart. If the cart does not exist, a new cart will be created.
Anchor to example-cart.updateselecteddeliveryoptionsCart.updateSelectedDeliveryOptions
Update the selected delivery options in the cart. Only available for carts associated with a customer access token.