Product Catalog

Collections, categories, tags and featured

The ways a store groups its products, and the calls that render them. Each of these pages shows its products through products.list, so filters, facets, sorting and paging work the same everywhere (see Products, filters and sorting). Every count is a count of public products, and a category, tag or collection with none is not listed.

Categories

categories.tree({ depth }) answers the category tree, 1 to 10 levels deep (default 10). Each node has id, handle, name, description, seoTitle, productCount (products in the category or below it) and children.

categories.get(handle) answers one category with its breadcrumbs (from the top of the tree down to it) and its children. Its products come from products.list({ category }), which includes the products of every subcategory. A product page carries breadcrumbs too: the path of the product's first category. An unknown handle answers 404 NOT_FOUND.

A menu and a category page
import { createCatalogClient, type CategoryNode } from '@usethrottle/catalog';

const catalog = createCatalogClient({
  publishableKey: 'pc_pk_your_browser_key',
});

// The menu: two levels of the tree, with public product counts.
const { categories } = await catalog.categories.tree({ depth: 2 });

function menu(nodes: CategoryNode[], indent = ''): void {
  for (const node of nodes) {
    console.log(`${indent}${node.name} (${node.productCount})`);
    menu(node.children, `${indent}  `);
  }
}
menu(categories);

// A category page: its breadcrumb, its subcategories and its products
// (products of every subcategory are included).
const page = await catalog.categories.get('brake-pads');
const trail = page.breadcrumbs.map((crumb) => crumb.name).join(' / ');
const products = await catalog.products.list({
  category: page.category.handle,
  facets: true,
});

console.log(trail, page.children.length, products.page.total);

Collections

The merchant builds collections in Storefront › Collections. A manual collection is a hand-ordered list of up to 5,000 products; a rule collection holds whatever matches its conditions (brand, type, tag, category, attribute, option, price, featured, availability, how recently created), checked at the time of each request. Each has its own handle, title, description, image, SEO fields and default sort, and only published collections are answered.

  • collections.list({ limit, cursor }) pages through the published collections (50 a page by default, up to 100) with their kind, image and productCount.
  • collections.get(handle) answers the collection page: title, description, descriptionHtml (sanitised by the catalog), image, seo, kind and defaultSort.
  • products.list({ collection }) answers its products. Without a sort they come in the collection's default sort; manual is a manual collection's own order.
  • An unpublished or unknown collection answers 404 NOT_FOUND. A published collection with no products still answers its page, with an empty product list.
A collection page
import { createCatalogClient } from '@usethrottle/catalog';

const catalog = createCatalogClient({
  publishableKey: 'pc_pk_your_browser_key',
});

export async function collectionPage(handle: string) {
  const answer = await catalog.collections.get(handle);

  // A renamed collection answers its new handle: send a permanent redirect.
  if ('redirectTo' in answer) {
    return { redirect: `/collections/${answer.redirectTo}` };
  }

  // Products in the collection's own order: its default sort applies when
  // you send no `sort`.
  const list = await catalog.products.list({
    collection: answer.collection.handle,
    limit: 24,
  });

  return {
    title: answer.collection.title,
    // Sanitised by the catalog, like product descriptions.
    descriptionHtml: answer.collection.descriptionHtml,
    seo: answer.collection.seo,
    products: list.products,
  };
}

Renamed handles redirect

When the merchant renames a product or a collection, the old handle keeps working. Asking for it answers { redirectTo } with the new handle instead of the product or collection. It is a normal answer, not an error: check for it, and send the shopper (and search engines) to the new URL with a permanent redirect. TypeScript makes you handle it, because the answer type is a union: products.get, collections.get and reviews.list all answer either their content or { redirectTo }.

Tags

tags.list({ q, limit }) answers tags with their product counts, most used first: all of them, or those starting with q (case-insensitive). The default is 50 tags and the most is 200. products.list({ tags }) answers products carrying any of the tags.

Collections, tags and tagged products
import { createCatalogClient } from '@usethrottle/catalog';

const catalog = createCatalogClient({
  publishableKey: 'pc_pk_your_browser_key',
});

// Every published collection, a page at a time.
let cursor: string | undefined;
do {
  const page = await catalog.collections.list({
    limit: 50,
    ...(cursor === undefined ? {} : { cursor }),
  });
  for (const collection of page.collections) {
    console.log(collection.handle, collection.kind, collection.productCount);
  }
  cursor = page.nextCursor ?? undefined;
} while (cursor !== undefined);

// Tags that start with "win", most used first.
const { tags } = await catalog.tags.list({ q: 'win', limit: 20 });

// Products carrying any of these tags.
const winter = await catalog.products.list({
  tags: tags.slice(0, 3).map((item) => item.tag),
});
console.log(winter.page.total);