Section Rendering API: ?sections= ve ?section_id=

Shopify section’larının HTML’ini ?sections= ile beşe kadar, ?section_id= ile tek tek, istediğiniz sayfanın bağlamında çekin; sayfayı yenilemeden güncelleyin.

Birden fazla
?sections=a,b (JSON, en fazla 5)
Tek
?section_id=a (HTML)
Bağlam
Çağırdığınız adres
Plan
Her plan
Son kontrol

Kısaca

Herhangi bir vitrin adresine ?sections=<id>,<id> eklediğinizde Shopify, o sayfanın bağlamında işlenmiş en fazla beş section’ın HTML’ini JSON olarak döndürür; ?section_id=<id> ise tek bir section’ı düz HTML olarak verir. Temalar bunu filtrelemeden sonra ürün listesini yenilemek, sayfa yenilemeden sayfalamak ya da sepet çekmecesini güncellemek için kullanır. q ve page gibi parametreler geçerliliğini korur. Section ID’si section.id’den gelir.

Nasıl kullanılır

  1. Section ID’sini bulun: Liquid’de {{ section.id }} ya da HTML’de id="shopify-section-…" sarmalayıcısı.
  2. Section’ı, ihtiyaç duyduğu verinin bulunduğu sayfada isteyin: /collections/summer?sections=<id>&page=2.
  3. shopify-section-<id> öğesini dönen HTML ile değiştirin.
  4. Sepet değişikliklerinde section’ları /cart/add.js ya da /cart/change.js çağrısının içinde isteyin (bundled section rendering).

Kod

refresh-section.js
// Re-renders one section in the context of the current page (for example a
// product grid after a filter change) without reloading the page.
// A section that fails to render comes back as null with HTTP 200.
async function refreshSection(sectionId, search = window.location.search) {
  const params = new URLSearchParams(search);
  params.set('sections', sectionId);
  const response = await fetch(`${window.location.pathname}?${params}`);
  if (!response.ok) throw new Error(`Section Rendering API: HTTP ${response.status}`);
  const sections = await response.json();
  const html = sections[sectionId];
  if (html == null) throw new Error(`Section ${sectionId} did not render`);
  document.getElementById(`shopify-section-${sectionId}`).outerHTML = html;
}

refreshSection('template--5678__main').catch((error) => console.error(error.message));
urls.txt
# Up to five sections as JSON, rendered in the context of the page you call
https://your-store.com/?sections=header,footer
https://your-store.com/collections/summer?sections=template--5678__main&page=2
https://your-store.com/products/example-product?sections[]=template--5678__main&sections[]=sections--1234__header

# One section as plain HTML (404 if the section ID doesn't exist)
https://your-store.com/products/example-product?section_id=template--5678__main

# Section IDs
static section file sections/social.liquid   ->  social
JSON template section                        ->  template--5678__image_banner
section group section                        ->  sections--1234__header
in Liquid: {{ section.id }}   in HTML: id="shopify-section-<section ID>"

Lintel tarafından yazıldı. MIT lisansı: kendi mağazanızda da müşteri işlerinizde de ücretsiz kullanabilirsiniz.

Sınırlar ve özel durumlar

  • JSON şablonlarındaki ve section gruplarındaki section’ların ID’leri template--5678__main gibi otomatik üretilir; sabit yazmak yerine section.id’den okuyun.
  • ?sections= ile işlenemeyen bir section HTTP 200 içinde null olarak gelir; ?section_id= bilinmeyen bir ID için 404 döndürür.
  • Ayar değeri gönderemezsiniz; section kayıtlı ayarlarıyla ya da varsayılanlarıyla işlenir.
  • Pazarın dil yolu korunsun diye adresleri Shopify.routes.root ya da location.pathname üzerinden kurun.

Sık sorulan sorular

Shopify’da sayfayı yenilemeden tek bir section nasıl yenilenir?
Sayfa adresini ?sections=<section id> ile çekin ve dönen HTML’i shopify-section-<id> öğesinin yerine koyun.
Aynı anda kaç section isteyebilirim?
sections parametresiyle tek istekte en fazla beş.
sections ile section_id arasındaki fark nedir?
sections bir ile beş arası section için JSON döndürür; section_id tek bir section’ı düz HTML olarak verir.
Section ID’sini nerede bulurum?
Liquid’de section.id. Sayfa HTML’inde sarmalayıcının id’sindeki shopify-section- ifadesinden sonraki kısım. Statik bir section için dosya adı.
  • Gösterilen tarihte bir Shopify mağazasında denendi. Shopify yönetim panelini sık değiştirir; bir adım farklı görünüyorsa fikir genellikle hâlâ geçerlidir.
  • Örneklerde yer tutucu ID’ler ve your-store.myshopify.com kullanılır. Kendi değerlerinizle değiştirin.
  • Kod parçaları MIT lisanslıdır.

Mağazanız için kurulmasını, test edilmesini ve bakımını isterseniz: Shopify geliştirme.

Geliştirmek istediğiniz bir şey mi var? Gelin, birlikte konuşalım.

Aynı anda sınırlı sayıda proje alıyoruz.

Gencer KarakayaShopify mühendisi

Proje özetinizi bir mühendis okur; size e-postayla yanıt veririz.