shopify-cartcomponent
The cart component provides a mini shopping cart functionality for your website. Here's how it works:
Add a
button
component inside a shopify-context component that's associated with a product.Call the
method in the
button
component'sonclick
attribute to add the product to the customer's cart.- This method requires an event object or a product data object.
- If using an event, the event target must be inside a product context component.
Display the cart using a native HTML
<dialog>
element.- To show it as a popup modal, call the
method.
- To show it as a popup modal, call the
Apply discount codes automatically using the
attribute.
- Pass a comma-separated list of discount codes that will be automatically applied to the cart.
Customize the cart's styling and content with CSS parts and slots. View examples
The cart component doesn't support mixing products from multiple stores.
Anchor to attributes and propertiesAttributes and properties
A method to add an item to the cart. The source can either be an event triggered from an element within a product context or a product object with a variant id.
A method to open the checkout popup window.
A method to close the cart dialog.
A property to get the open state of the cart.
Example:
A method to display the cart as a modal in a dialog
element modelessly.
A method to display the underlying cart as a modal in a dialog
element.
The target attribute for the checkout link. Defaults to "_top".
Anchor to css partsCSS parts
CSS parts allow you to target and override the default styling within the cart component.
The dialog element.
Discount code label.
Discount error message.
Input field. Used to style the input field that applies a discount code.
The cart line-item title element.
The cart line-item image element.
The cart line-item options element.
The cart line-item quantity element.
The primary button element. Used to style the checkout link.
The secondary button element. Used to style the buttons that modify a cart-line item.
The tertiary button element. Used to style the button that applies a discount code.
Anchor to slotsSlots
Slots allow you to override the default content of the cart component.
The content to display in the apply discount button. Useful to add a custom apply discount button text.
The content to display in the checkout button. Useful to add a custom checkout button text.
The content to display when a discount code cannot be applied. Useful to add a custom error message for internationalization
The title of the discount section. Useful to add a custom discount section title for internationalization
The content to display when the cart is empty.
Extend the cart with additional content below the checkout button. Useful to add upsell products or other content.
Example
HTML
Anchor to examplesExamples
Anchor to example-customize-the-cart-with-css-parts-and-slotsCustomize the cart with CSS parts and slots
Add <div>
tags with slot
attributes to show custom content on the empty state and checkout button. Add a <style>
tag to include CSS parts that change the default styling of the cart's dialog, primary buttons, and secondary buttons.
Anchor to example-manually-add-a-product-to-the-cartManually add a product to the cart
Add a product to the cart without a product context. This is useful if you already have a product GID.
Anchor to example-apply-discount-codes-automaticallyApply discount codes automatically
Automatically apply discount codes to the cart by setting the discount-codes
attribute. The discount codes will be applied whenever items are added, removed, or updated in the cart.