Skip to main content

shopify-cart

The cart component provides a mini shopping cart functionality for your website. Here's how it works:

  1. Add a button component inside a shopify-context component that's associated with a product.

  2. Call the addLine() method in the button component's onclick 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.
  3. Display the cart using a native HTML <dialog> element.

    • To show it as a popup modal, call the showModal() method.
  4. Apply discount codes automatically using the discountCodes attribute.

    • Pass a comma-separated list of discount codes that will be automatically applied to the cart.
  5. Customize the cart's styling and content with CSS parts and slots. View examples

Note

The cart component doesn't support mixing products from multiple stores.

Anchor to attributes and propertiesAttributes and properties

Anchor to addLine
addLine
(source: ) =>

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.

Anchor to close
close
() =>

A method to close the cart dialog.

boolean

A property to get the open state of the cart.

Example: getElementById('cart').open

() =>

A method to display the cart as a modal in a dialog element modelessly.

Anchor to showModal
showModal
() =>

A method to display the underlying cart as a modal in a dialog element.

Anchor to target
target
string

The target attribute for the checkout link. Defaults to "_top".

CSS parts allow you to target and override the default styling within the cart component.

Anchor to dialog
dialog

The dialog element.

Anchor to discount-code
discount-code

Discount code label.

Anchor to discount-error
discount-error

Discount error message.

Anchor to input-field
input-field

Input field. Used to style the input field that applies a discount code.

Anchor to line-heading
line-heading

The cart line-item title element.

Anchor to line-image
line-image

The cart line-item image element.

Anchor to line-options
line-options

The cart line-item options element.

Anchor to line-price
line-price

The cart line-item quantity element.

Anchor to primary-button
primary-button

The primary button element. Used to style the checkout link.

Anchor to secondary-button
secondary-button

The secondary button element. Used to style the buttons that modify a cart-line item.

Anchor to tertiary-button
tertiary-button

The tertiary button element. Used to style the button that applies a discount code.

Slots allow you to override the default content of the cart component.

Anchor to apply-discount-button
apply-discount-button

The content to display in the apply discount button. Useful to add a custom apply discount button text.

Anchor to checkout-button
checkout-button

The content to display in the checkout button. Useful to add a custom checkout button text.

Anchor to discount-apply-error
discount-apply-error

The content to display when a discount code cannot be applied. Useful to add a custom error message for internationalization

Anchor to discounts-title
discounts-title

The title of the discount section. Useful to add a custom discount section title for internationalization

Anchor to empty
empty

The content to display when the cart is empty.

Anchor to extension
extension

Extend the cart with additional content below the checkout button. Useful to add upsell products or other content.

Examples

HTML

<shopify-store
store-domain="https://your-store.myshopify.com"
>
</shopify-store>

<!-- The context is bound to the store -->
<shopify-context
type="product"
handle="handle-of-product"
>
<template>
<shopify-variant-selector></shopify-variant-selector>
<!-- The product added will be whatever
variant is selected for the context product handle -->
<button
onclick="getElementById('cart').addLine(event).showModal();"
>
Add to cart
</button>
</template>
</shopify-context>

<shopify-cart id="cart"></shopify-cart>
Was this page helpful?