Overview
This is the SDK equivalent of Rendering blocks in Liquid. The shape of the section is the same: one Liquid section, one merchandiser-facing block-picker setting. The SDK’sblocks() controller does the HTTP call, deduplicates in-flight requests, caches results in localStorage, and exposes the products on reactive state.
You do not need to handle cart context, click feedback, or cross-block product deduplication. The Layers theme app extension handles those for every Blocks request on the storefront.
Use this version when you have already installed @commerce-blocks/sdk in your theme. For a no-dependencies version, see the Liquid + Fetch guide.
Prerequisites
- The Layers Shopify app installed and the theme app extension enabled in Online Store → Themes → Customize → App embeds.
- The SDK installed and initialized in your theme. See SDK installation.
The section
sections/layers-recommendations.liquid is the same Liquid scaffold as the Fetch version: a section with a block-picker setting and a single placeholder element carrying the selected block ID.
select options are populated from block metaobjects. See the Fetch guide for the pattern that mirrors metaobjects into the dropdown so merchandisers pick by name instead of pasting a ULID.
Hydrating the section
For each rendered section, create ansdk.blocks() controller with the selected block ID and the page anchor. Subscribe to the state and render on data changes.
What the app embed handles for you
When the Layers theme app extension is enabled, the storefront-side embed augments every Blocks request, including the SDK’s, so you do not have to:- Cart context: current cart line items are sent automatically; cart-anchored blocks work without you reading
/cart.js. - Product deduplication: products rendered earlier on the page are excluded from later blocks automatically.
- Click attribution and feedback: the embed listens for clicks on rendered cards and submits feedback to keep ranking models fresh.
- Identity: the storefront pixel manages
deviceIdandsessionId.
Anchors
sdk.blocks({ blockId, anchorId }) resolves the right value based on template:
Both numeric IDs and Shopify GIDs are accepted on product anchors; both IDs and handles are accepted on collection anchors.
Why the SDK over Fetch
Cart context, dedup, and attribution behavior is identical between the two: the theme app extension does that work either way.