Overview
This is the SDK equivalent of Rendering sort orders in Liquid. You still render the dropdown from sort metaobjects in Liquid so the UI stays in sync with the dashboard. Pass the selectedcode to the SDK’s collection() or search() controller as sort instead of issuing the API request yourself.
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
- Sort orders configured in the Layers dashboard with Enable as Storefront Sort turned on.
- The SDK installed and initialized in your theme. See SDK installation.
Collection sort dropdown
1
Render the dropdown from sort metaobjects
Filter by the
collection scope and order by the configured order field. Read the collection’s default sort from layers.default_sort_order and the active sort from the URL.2
Drive a collection controller from the dropdown
Create the controller once, subscribe to render, and call Passing
get({ sort }) whenever the selection changes. The SDK deduplicates in-flight requests and updates the state. Subscribers re-render automatically.sort to the controller sets the default. Call get({ sort: undefined }) to fall back to that default, or pass a new sort to override it.Search sort dropdown
The pattern is identical. Filter the metaobjects by thesearch scope and drive an sdk.search() controller instead.
Fetching sort orders at runtime
If you do not want to render the dropdown from Liquid metaobjects, you can fetch available sort orders at runtime withsdk.sortOrders():
Combining sort with filters and pagination
Controller options shallow-merge acrossget() calls. Passing only the field you want to change keeps the rest sticky.
Default sort orders
Pass the metafield-resolved code (read in Liquid) as
sort to the controller so the SDK and the rendered dropdown agree on the default.
Why the SDK over Fetch
Troubleshooting
Selecting a sort does not change the grid. Confirm the subscriber renders on everydata change, not just the first one. The controller emits a new state on every get().
The wrong default is pre-selected. Read request.params.sort and the layers.default_sort_order metafield in Liquid, mark the matching <option> as selected server-side, and pass the same code as sort to the controller.
Sort options are missing from the dropdown. Check Enable as Storefront Sort is on in the Layers dashboard and the metaobject’s scope array contains the right surface.