I want to know how to develop an app with filtering capabilities, like “search & discovery”, which can let you use a filtered collection in the collection template. I just can’t find any documents about it.
Hi Wenjian,
You can use the collections
query to fetch collections and apply filters. For example:
query FilteredCollections {
collections(first: 5, query: "title:Music") {
edges {
node {
id
title
handle
updatedAt
sortOrder
}
}
}
}
To display filtered collections in the Shopify admin, you can use the ResourceList component from Admin UI extensions. This component allows merchants to filter and interact with collections. EG:
import React from 'react';
import {extend, render, ResourceList, ResourceItem} from '@shopify/admin-ui-extensions-react';
function App() {
return (
<ResourceList
filterControl={{
queryValue: '',
queryPlaceholder: 'Filter collections',
onQueryChange: (queryValue) => console.log('Filtering for', queryValue),
onQueryClear: () => console.log('Clear filters'),
}}
>
<ResourceItem id="1234" onPress={() => console.log('Pressed 1')}>
Collection 1
</ResourceItem>
<ResourceItem id="5678" onPress={() => console.log('Pressed 2')}>
Collection 2
</ResourceItem>
</ResourceList>
);
}
extend(
'Playground',
render(() => <App />),
);
To use filtered collections in a collection template, you can leverage metafields. For example, you can create a metafield definition for collections and use it as a condition in templates. Use the standardMetafieldDefinitionEnable
mutation to enable a metafield definition:
mutation EnableMetafieldDefinition {
standardMetafieldDefinitionEnable(
id: "gid://shopify/StandardMetafieldDefinitionTemplate/2",
ownerType: COLLECTION,
pin: false,
visibleToStorefrontApi: true
) {
createdDefinition {
name
key
namespace
description
}
userErrors {
field
message
}
}
}
Hopefully this helps for exploring options to support your app project!
The other option would be syncing all the store data (products/collections) to your store into a database and building the filtering out yourself, this would most likely allow for more filtering options, but based on the database setup you go with.
You would most likely then use something like a theme app extension to output the product list and filters on the collection/search page.