Vue Storefront is now Alokai! Learn More
Category

Category

The Unified Data Layer provides methods that let you query categories by id or slug.

Coverage

MethodCommercetoolsSAPCCBigCommerceSFCCMagento
GetCategories
GetCategory

getCategories

getCategories fetches a list of categories from your commerce platform. By default, it returns all categories, but you can filter the response based on ids or slugs.

Usage

// fetching all categories
const categories = await sdk.unified.getCategories();

// filter for categories matching an id
const categories = await sdk.unified.getCategories({
  ids: ["220040"],
});

// filter for categories matching a slug
const categories = await sdk.unified.getCategories({
  slugs: ["shirts"],
});

// filter for categories matching an id OR a slug
const categories = await sdk.unified.getCategories({
  ids: ["220040"],
  slugs: ["shirts"],
});

Types

export type GetCategories = (args?: GetCategoriesArgs) => Promise<SfCategory[]>;

export type GetCategoriesArgs = {
  ids?: string[];
  slugs?: string[];
};

export interface SfCategory {
  id: SfId;
  name: string;
  slug: string;
  subcategories: Maybe<SfCategory[]>;
  productCount: Maybe<number>;
}

getCategory

getCategory fetches a specific category using its id. It returns a category and its ancestors.

Usage

const { ancestors, category } = await sdk.unified.getCategory({
  id: "220040",
});

Type

export type GetCategoryArgs = {
  id: string;
};

export type GetCategory = (args: GetCategoryArgs) => Promise<{
  ancestors: SfCategory[];
  category: SfCategory;
}>;