Skip to content
Hekana Spray
Documentation
Getting started
Theme development
Components and tags

Localization

Apps and custom code

Examples and recipes
API reference
Publishing
Build apps
AI development

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.

Result

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.

sections/cart-main.snfSneferu
<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>
templates/cart.snfSneferu
<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

Result

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.

SettingLabelTypeDefaultNotes
titleHeadingtext&#123;"ar":"سلة المشتريات","en":"Your cart"&#125;set per language
summary_positionSummary positionselectsideone of side, below
subtotal_labelSubtotal labeltextset per language
checkout_labelCheckout buttontextset per language
noteNote under the totaltext&#123;"ar":"الشحن والخصومات بتتحسب في الخطوة الجاية.","en":"Shipping and discounts are worked out at checkout."&#125;set per language
show_continueShow continue shoppingbooleantrue
continue_labelContinue shopping texttext&#123;"ar":"كمل تسوق","en":"Continue shopping"&#125;set per language
continue_linkContinue shopping linklink/products
empty_imageEmpty cart imageimage
empty_titleEmpty cart titletext&#123;"ar":"سلتك فاضية","en":"Your cart is empty"&#125;set per language
empty_textEmpty cart texttextarea&#123;"ar":"ضيف منتجات وهتلاقيها هنا.","en":"Add products and they will show up here."&#125;set per language
empty_button_labelEmpty cart buttontext&#123;"ar":"ابدأ التسوق","en":"Start shopping"&#125;set per language
empty_button_linkEmpty cart button linklink/products
empty_button_styleEmpty cart button styleselectsolidone of solid, outline, ghost
padding_yVertical paddingrange400 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-items lists 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-summary prints the cart totals, and checkout-label on hk-cart-items sets the checkout button text from a setting.
  • hk-heading and hk-image are plain content tags around the cart controls, driven by the section settings.
  • templates/cart.snf lists the section, so the cart page is this section and nothing else (see Cart and controllers).

Next steps