Skip to main content

Collection filters

The collection filter functions help you build filter controls for a collection page. Use the functions to check the boxes for active filters, match a filter from the URL against a filter from the Storefront API, and build links that remove one filter.

Import filterEquals, isFilterInputActive, and getFilterRemovalUrl from @shopify/hydrogen.


Checks whether two product filters match.

Price filters match when the minimum and maximum are equal, including ranges that set only one bound.

ProductFilter
required

The first filter to compare.

ProductFilter
required

The second filter to compare.

Anchor to filterEquals-returnsReturns
boolean

true when both filters are the same kind with the same values. false for different kinds or unrecognized filters.

Examples

TypeScript

import { filterEquals, parseCollectionParams } from "@shopify/hydrogen";

const fromUrl = parseCollectionParams(
new URLSearchParams("filter.v.price.gte=10&filter.v.price.lte=100"),
).filters[0];
const fromApi = { price: { min: 10, max: 100 } };

const samePrice = filterEquals(fromUrl, fromApi);
const differentKind = filterEquals({ tag: "men" }, { productVendor: "Nike" });

Checks whether a filter value's input JSON string from the Storefront API matches an active filter. Use the result to set the checked state of a filter control.

The function compares filters the same way as filterEquals.

Anchor to activeFilters
activeFilters
ProductFilter[]
required

The active filters, such as filters from the collection state.

Anchor to input
input
string
required

The filter value's input JSON string.

true when the parsed input matches an active filter. false when the input matches no active filter or isn't valid JSON.

A filter that the Storefront API returns for the current collection or search results. Render filter controls, such as checkboxes, swatches, and sliders, from the filter's values.

string
required

ID of the filter.

Anchor to label
label
string
required

Name to display, such as "Color", "Size", or "Price".

required

Kind of input, such as a boolean toggle, a multi-select list, or a price range.

Anchor to values
values
<TValue>[]
required

Values that a customer can select, each with a label, a product count, and a filter input.

Anchor to presentation
presentation
| null

How the storefront displays the filter's values. The Storefront API returns a value only for LIST filters and null for other filters.

Examples

TypeScript

import { isFilterInputActive, type AvailableFilter, type CollectionState } from "@shopify/hydrogen";

declare const state: CollectionState;
declare const availableFilters: AvailableFilter[];

const checkboxes = availableFilters.flatMap((filter) =>
filter.values.map((value) => ({
label: value.label,
count: value.count,
checked: isFilterInputActive(state.filters, value.input),
})),
);

Builds a query string from the current URL params, minus the given filter. Use the string in plain remove-filter links.

The function keeps every other param and drops the before and after pagination cursors, which sends the customer to the first page of results.

Anchor to currentParams
currentParams
URLSearchParams
required

The current URL search params.

Anchor to filter
filter
ProductFilter
required

The filter to remove from the URL.

A query string that starts with ?. The string is ? alone when no params remain.

Examples

TypeScript

import {
getFilterRemovalUrl,
serializeCollectionParams,
type CollectionState,
type ProductFilter,
} from "@shopify/hydrogen";

export function filterRemovalHref(
collectionPath: string,
state: CollectionState,
filter: ProductFilter,
) {
const currentParams = serializeCollectionParams(state);
const removal = getFilterRemovalUrl(currentParams, filter);
return removal === "?" ? collectionPath : `${collectionPath}${removal}`;
}

Was this page helpful?