component-cart-drawer Snippet
snippets/component-cart-drawer.liquid renders the slide-out cart drawer with line items, totals, cart note, and checkout actions. It pairs Alpine.js state (cartOpen) with the <cart-drawer> custom element and wires Liquid Ajax Cart bindings for real-time updates.
What It Does
- Defines a documented snippet (
{% doc %}) that merchants can include anywhere (usually the header/layout). - Loads
cart.cssfor drawer styling and outputs the<cart-drawer>wrapper with Alpine classes/attributes. - Handles both empty and populated cart states, including localized messaging and optional featured collection.
- Iterates
cart.itemsto render media, pricing, options, custom properties, discounts, and quantity controls. - Includes footer content: cart note, cart discounts, estimated totals, and the checkout form.
- Imports
component-cart-drawer.jsandcart-discount.jsmodules for interactivity and discount handling.
Dependencies & Assets
| Type | Files / Components |
|---|---|
| CSS | cart.css |
| JS | component-cart-drawer.js, cart-discount.js |
| Snips | cart-discount, inline SVG icons (checkmark, minus, plus, discount, etc.) |
| Data | Requires global cart, settings.cart_color_scheme, settings.cart_drawer_collection, settings.show_cart_note |
- Alpine.js powers
cartOpen/noteOpenstate toggles (expects surrounding layout to define Alpine component). - AJAX bindings (
data-ajax-cart-*) rely on Liquid Ajax Cart to keep contents in sync.
Markup Overview
liquid
<cart-drawer
id="cart-drawer"
class="color-{{ settings.cart_color_scheme }}"
:class="{ 'cart-open': cartOpen }"
>
<button class="drawer-overlay" @click="cartOpen = !cartOpen">…</button>
<div class="drawer__wrapper" data-ajax-cart-section>
{% if cart == empty %}
<!-- Empty cart content -->
{% else %}
<!-- Cart header, items, footer -->
{% endif %}
</div>
</cart-drawer>
<script src="{{ 'component-cart-drawer.js' | asset_url }}" type="module"></script>
<script src="{{ 'cart-discount.js' | asset_url }}" type="module"></script>- Wrapper uses
color-\{\{ settings.cart_color_scheme \}\}for theme integration. - Overlay button closes the drawer and is accessible via visually hidden text.
data-ajax-cart-sectionensures the drawer updates after AJAX actions.
Empty State
- Shows a close icon button, “cart empty” heading (
sections.cart.empty),Continue shoppinglink, and optional login prompt/statements for guests. - If
settings.cart_drawer_collectionis set, displays the featured collection image/title as cross-sell content.
Populated Cart
Header
- Title uses
cart.titletranslation and outputscart.item_count. - Close button toggles Alpine state.
Items
- Loop over
cart.items:- Media: product image or placeholder.
- Details: title link, original/final price, options/properties/selling plan, line-level discounts with icons.
- Quantity controls inside
<ajax-cart-quantity>withdata-ajax-cart-quantity-*bindings. - Remove link uses
line_item.url_to_removeplus an inline close icon. - Totals show compare vs final line price.
- Each item includes an
errorsblock withdata-ajax-cart-errors.
Footer
- Optional cart note textarea toggled by Alpine
noteOpen. - Renders
cart-discountsnippet (discount code input/pills). - Summary shows cart-level discounts, estimated total, and shipping/tax disclaimer.
- Checkout form posts via
form 'cart'.
Events & Behavior
<cart-drawer>listens for custom events viacomponent-cart-drawer.js(see asset doc) to open automatically after AJAX add-to-cart.data-ajax-cart-section-scrollon.cart-itemsenables automatic scroll-to-top when contents update.data-ajax-cart-request-buttonensures remove links trigger AJAX refreshes.
Usage Tips
- Mount the snippet once (typically in
snippets/header.liquidorlayout/theme.liquid) so IDs remain unique. - Ensure Alpine component defines
cartOpenstate and listens for the globalcart-openevent (dispatched by the JS module). - Provide translations for all referenced keys (
cart.title,cart.remove,cart.checkout, etc.). - Keep icon assets (
icon-close.svg,icon-discount.svg, etc.) available inassets/. - When customizing layout, maintain
data-ajax-cart-attributes so Liquid Ajax Cart keeps working.