Product details
The starter theme product section from Sneferu with the blocks for title, price, variants and buy button, with its complete code.
On this page
What you'll build
The product page: gallery, breadcrumb and an info column built from blocks. 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: Page types, Blocks, Product gallery, Variants, Add to cart.
These files come from the starter theme that npx sneferu init creates. The example is sections/product-main.snf.
<hk-section padding-y="{{ section.settings.padding_y }}" mobile-padding-y="24">
{% if section.settings.show_breadcrumb %}
<nav class="sf-breadcrumb" aria-label="{{ t('breadcrumb') }}">
<a href="{{ url('/') }}">{{ t('home') }}</a>
{% if product.category %}
<span aria-hidden="true">/</span>
<a href="{{ product.category.url }}">{{ product.category.title }}</a>
{% endif %}
<span aria-hidden="true">/</span>
<span aria-current="page">{{ product.title }}</span>
</nav>
{% endif %}
<div class="sf-product sf-gallery-{{ section.settings.gallery_side }} sf-sticky-{{ section.settings.sticky_info }}">
<div class="sf-product-gallery">
<hk-product-gallery product="{{ product }}" layout="{{ section.settings.gallery_layout }}" />
</div>
<div class="sf-product-info" role="group" aria-label="{{ t('product_info') }}">
{% for block in section.blocks %}<hk-block of="{{ block }}" />{% endfor %}
</div>
</div>
</hk-section>
<hk-schema>
{
"name": {
"en": "Product",
"ar": "المنتج"
},
"version": 1,
"templates": ["product"],
"blocks": {
"allowed": [
"product-title",
"product-price",
"product-excerpt",
"product-variants",
"product-buy",
"product-description",
"product-tags",
"product-category",
"product-text"
],
"max": 20
},
"settings": {
"gallery_layout": {
"type": "select",
"label": {
"en": "Gallery layout",
"ar": "شكل المعرض"
},
"options": ["thumbs-below", "thumbs-side", "stacked"],
"default": "thumbs-below"
},
"gallery_side": {
"type": "select",
"label": {
"en": "Gallery position",
"ar": "مكان الصور"
},
"options": ["start", "end"],
"default": "start"
},
"sticky_info": {
"type": "boolean",
"label": {
"en": "Keep the details in view while scrolling",
"ar": "تثبيت التفاصيل أثناء التمرير"
},
"default": false
},
"show_breadcrumb": {
"type": "boolean",
"label": {
"en": "Show breadcrumb",
"ar": "إظهار مسار الصفحة"
},
"default": true
},
"padding_y": {
"type": "range",
"label": {
"en": "Vertical padding",
"ar": "المسافة الرأسية"
},
"min": 0,
"max": 160,
"step": 8,
"default": 40
}
}
}
</hk-schema><hk-heading
text="{{ product.title }}"
level="{{ block.settings.level }}"
size="{{ block.settings.size }}"
mobile-size="{{ block.settings.mobile_size }}"
/>
<hk-schema>
{
"name": {
"en": "Title",
"ar": "العنوان"
},
"version": 1,
"settings": {
"level": {
"type": "select",
"label": {
"en": "Heading level",
"ar": "مستوى العنوان"
},
"options": ["h1", "h2", "h3"],
"default": "h1"
},
"size": {
"type": "range",
"label": {
"en": "Size",
"ar": "الحجم"
},
"min": 18,
"max": 64,
"default": 32
},
"mobile_size": {
"type": "range",
"label": {
"en": "Size on phones",
"ar": "الحجم على الموبايل"
},
"min": 16,
"max": 48,
"default": 26
}
}
}
</hk-schema><div class="sf-price-row">
{% render 'price', product: product, bind: true %}
{% if block.settings.show_badge %}
{% if product.available %}
{% if product.compare_at > product.price %}<span class="sf-badge">{{ t('sale') }}</span>{% endif %}
{% else %}
<span class="sf-badge sf-badge-muted">{{ t('sold_out_badge') }}</span>
{% endif %}
{% endif %}
</div>
<hk-schema>
{
"name": {
"en": "Price",
"ar": "السعر"
},
"version": 1,
"settings": {
"show_badge": {
"type": "boolean",
"label": {
"en": "Show sale and sold out badge",
"ar": "إظهار شارة الخصم ونفاد المخزون"
},
"default": true
}
}
}
</hk-schema><hk-variant-picker product="{{ product }}" display="{{ block.settings.style }}" />
<hk-schema>
{
"name": {
"en": "Variant picker",
"ar": "اختيار النوع"
},
"version": 1,
"settings": {
"style": {
"type": "select",
"label": {
"en": "Style",
"ar": "الشكل"
},
"options": ["auto", "swatches", "buttons", "select"],
"default": "auto"
}
}
}
</hk-schema><hk-add-to-cart
product="{{ product }}"
label="{{ block.settings.label }}"
sold-out-label="{{ block.settings.sold_out_label }}"
quantity="{{ block.settings.show_quantity }}"
variant="{{ block.settings.button_style }}"
/>
<hk-schema>
{
"name": {
"en": "Buy buttons",
"ar": "زر الشراء"
},
"version": 1,
"settings": {
"label": {
"type": "text",
"label": {
"en": "Button label (default: Add to cart)",
"ar": "نص الزر (الافتراضي: أضف للسلة)"
},
"default": "",
"localized": true,
"maxLength": 30
},
"sold_out_label": {
"type": "text",
"label": {
"en": "Sold out label",
"ar": "نص نفاد المخزون"
},
"default": "",
"localized": true,
"maxLength": 30
},
"show_quantity": {
"type": "boolean",
"label": {
"en": "Show quantity",
"ar": "إظهار الكمية"
},
"default": true
},
"button_style": {
"type": "select",
"label": {
"en": "Button style",
"ar": "شكل الزر"
},
"options": ["solid", "outline"],
"default": "solid"
}
}
}
</hk-schema>Download: sections/product-main.snf, blocks/product-title.snf, blocks/product-price.snf, blocks/product-variants.snf, blocks/product-buy.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/product-main.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
gallery_layout | Gallery layout | select | thumbs-below | one of thumbs-below, thumbs-side, stacked |
gallery_side | Gallery position | select | start | one of start, end |
sticky_info | Keep the details in view while scrolling | boolean | false | |
show_breadcrumb | Show breadcrumb | boolean | true | |
padding_y | Vertical padding | range | 40 | 0 to 160 in steps of 8 |
Text keys the code reads with t(). Change the English text in locales/en.json and the Arabic text under the same key in locales/ar.json.
| Key | English text |
|---|---|
breadcrumb | Breadcrumb |
home | Home |
product_info | Product details |
How it works
- This code uses the tags
hk-section,hk-product-gallery,hk-block,hk-heading,hk-variant-picker,hk-add-to-cart. hk-product-galleryshows the product photos in the layout the merchant picks.- The information column is just
{% for block in section.blocks %}withhk-block: the merchant orders the title, price, options and buy button blocks in the editor. hk-variant-picker(in the variants block) andhk-add-to-cart(in the buy block) are the controls that choose an option and add it to the cart.- The breadcrumb reads
product.categoryand prints text witht('breadcrumb')andt('home'). hk-headingin the title block prints the product title as the page heading.
