Cart page
The starter theme cart section from Sneferu with its empty state part, the complete code and the settings a merchant can change.
On this page
What you'll build
The cart: line items, an empty state, a summary and a checkout button. The Result below is drawn by the real Sneferu renderer from the code on this page.
Requirements
- Node.js 20 or newer
- A theme made with
npx sneferu init. Every file below is part of it. - The preview running:
npx sneferu dev
Code
Concepts this teaches: Cart, Parts, Empty states, Checkout button.
These files come from the starter theme that npx sneferu init creates. The example is sections/cart-main.snf.
<hk-section padding-y="{{ section.settings.padding_y }}" mobile-padding-y="24">
{% if section.settings.title %}
<hk-heading text="{{ section.settings.title }}" level="h1" size="32" mobile-size="26" />
{% endif %}
<div class="sf-cart sf-cart-{{ section.settings.summary_position }}">
<hk-cart-items checkout-label="{{ section.settings.checkout_label }}">
<hk-part name="empty">
<div class="sf-empty">
{% if section.settings.empty_image %}
<hk-image src="{{ section.settings.empty_image }}" alt="" width="240" />
{% endif %}
{% if section.settings.empty_title %}
<h2 class="sf-empty-title">{{ section.settings.empty_title }}</h2>
{% endif %}
{% if section.settings.empty_text %}
<p>{{ section.settings.empty_text }}</p>
{% endif %}
{% render 'button', label: section.settings.empty_button_label, href: section.settings.empty_button_link, style: section.settings.empty_button_style %}
</div>
</hk-part>
</hk-cart-items>
<aside class="sf-cart-aside">
<hk-cart-summary
subtotal-label="{{ section.settings.subtotal_label }}"
checkout-label="{{ section.settings.checkout_label }}"
/>
{% if section.settings.note %}
<p class="sf-muted">{{ section.settings.note }}</p>
{% endif %}
{% if section.settings.show_continue %}
{% render 'button', label: section.settings.continue_label, href: section.settings.continue_link, style: 'ghost' %}
{% endif %}
</aside>
</div>
</hk-section>
<hk-schema>
{
"name": {
"en": "Cart",
"ar": "السلة"
},
"version": 1,
"templates": ["cart"],
"settings": {
"title": {
"type": "text",
"label": {
"en": "Heading",
"ar": "العنوان"
},
"default": {
"ar": "سلة المشتريات",
"en": "Your cart"
},
"localized": true,
"maxLength": 60
},
"summary_position": {
"type": "select",
"label": {
"en": "Summary position",
"ar": "مكان الملخص"
},
"options": ["side", "below"],
"default": "side"
},
"subtotal_label": {
"type": "text",
"label": {
"en": "Subtotal label",
"ar": "نص المجموع"
},
"default": "",
"localized": true,
"maxLength": 30
},
"checkout_label": {
"type": "text",
"label": {
"en": "Checkout button",
"ar": "زر إتمام الشراء"
},
"default": "",
"localized": true,
"maxLength": 30
},
"note": {
"type": "text",
"label": {
"en": "Note under the total",
"ar": "ملاحظة تحت المجموع"
},
"default": {
"ar": "الشحن والخصومات بتتحسب في الخطوة الجاية.",
"en": "Shipping and discounts are worked out at checkout."
},
"localized": true,
"maxLength": 140
},
"show_continue": {
"type": "boolean",
"label": {
"en": "Show continue shopping",
"ar": "إظهار متابعة التسوق"
},
"default": true
},
"continue_label": {
"type": "text",
"label": {
"en": "Continue shopping text",
"ar": "نص متابعة التسوق"
},
"default": {
"ar": "كمل تسوق",
"en": "Continue shopping"
},
"localized": true,
"maxLength": 30,
"visible_if": {
"setting": "show_continue",
"equals": true
}
},
"continue_link": {
"type": "link",
"label": {
"en": "Continue shopping link",
"ar": "رابط متابعة التسوق"
},
"default": "/products",
"visible_if": {
"setting": "show_continue",
"equals": true
}
},
"empty_image": {
"type": "image",
"label": {
"en": "Empty cart image",
"ar": "صورة السلة الفاضية"
},
"default": ""
},
"empty_title": {
"type": "text",
"label": {
"en": "Empty cart title",
"ar": "عنوان السلة الفاضية"
},
"default": {
"ar": "سلتك فاضية",
"en": "Your cart is empty"
},
"localized": true,
"maxLength": 80
},
"empty_text": {
"type": "textarea",
"label": {
"en": "Empty cart text",
"ar": "نص السلة الفاضية"
},
"default": {
"ar": "ضيف منتجات وهتلاقيها هنا.",
"en": "Add products and they will show up here."
},
"localized": true,
"maxLength": 200
},
"empty_button_label": {
"type": "text",
"label": {
"en": "Empty cart button",
"ar": "زر السلة الفاضية"
},
"default": {
"ar": "ابدأ التسوق",
"en": "Start shopping"
},
"localized": true,
"maxLength": 30
},
"empty_button_link": {
"type": "link",
"label": {
"en": "Empty cart button link",
"ar": "رابط زر السلة الفاضية"
},
"default": "/products"
},
"empty_button_style": {
"type": "select",
"label": {
"en": "Empty cart button style",
"ar": "شكل زر السلة الفاضية"
},
"options": ["solid", "outline", "ghost"],
"default": "solid"
},
"padding_y": {
"type": "range",
"label": {
"en": "Vertical padding",
"ar": "المسافة الرأسية"
},
"min": 0,
"max": 160,
"step": 8,
"default": 40
}
}
}
</hk-schema><hk-sections />
<hk-schema>
{
"sections": [
{
"type": "cart-main"
}
]
}
</hk-schema>Download: sections/cart-main.snf, templates/cart.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/cart-main.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
title | Heading | text | {"ar":"سلة المشتريات","en":"Your cart"} | set per language |
summary_position | Summary position | select | side | one of side, below |
subtotal_label | Subtotal label | text | set per language | |
checkout_label | Checkout button | text | set per language | |
note | Note under the total | text | {"ar":"الشحن والخصومات بتتحسب في الخطوة الجاية.","en":"Shipping and discounts are worked out at checkout."} | set per language |
show_continue | Show continue shopping | boolean | true | |
continue_label | Continue shopping text | text | {"ar":"كمل تسوق","en":"Continue shopping"} | set per language |
continue_link | Continue shopping link | link | /products | |
empty_image | Empty cart image | image | ||
empty_title | Empty cart title | text | {"ar":"سلتك فاضية","en":"Your cart is empty"} | set per language |
empty_text | Empty cart text | textarea | {"ar":"ضيف منتجات وهتلاقيها هنا.","en":"Add products and they will show up here."} | set per language |
empty_button_label | Empty cart button | text | {"ar":"ابدأ التسوق","en":"Start shopping"} | set per language |
empty_button_link | Empty cart button link | link | /products | |
empty_button_style | Empty cart button style | select | solid | one of solid, outline, ghost |
padding_y | Vertical padding | range | 40 | 0 to 160 in steps of 8 |
How it works
- This code uses the tags
hk-section,hk-heading,hk-cart-items,hk-part,hk-image,hk-cart-summary,hk-sections. hk-cart-itemslists the lines of the visitor's cart and handles quantity changes and removal for you.hk-part name="empty"is the markup shown when the cart has no items: you design the empty state, the tag decides when to show it.hk-cart-summaryprints the cart totals, andcheckout-labelonhk-cart-itemssets the checkout button text from a setting.hk-headingandhk-imageare plain content tags around the cart controls, driven by the section settings.templates/cart.snflists the section, so the cart page is this section and nothing else (see Cart and controllers).
