ProductCategoriesResource
To use this resource, make sure to enable its feature flag: product_categories
This class is used to send requests to Store Product Category API Routes. All its method
are available in the JS Client under the medusa.productCategories
property.
Products can be categoriezed into categories. A product can be associated more than one category. Using the methods in this class, you can list or retrieve a category's details and products.
Related Guide: How to use product categories in a storefront.
Methods
list
Retrieve a list of product categories. The product categories can be filtered by fields such as handle
or q
passed in the query
parameter.
The product categories can also be paginated. This method can also be used to retrieve a product category by its handle.
Example
To list product categories:
To retrieve a product category by its handle:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.productCategories.list({
handle: "women",
})
.then(({ product_categories, limit, offset, count }) => {
if (!product_categories.length) {
// category does not exist
}
const category = product_categories[0]
})
To specify relations that should be retrieved within the product categories:
By default, only the first 100
records are retrieved. You can control pagination by specifying the limit
and offset
properties:
Parameters
Filters and pagination configurations to apply on the retrieved product categories.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<StoreGetProductCategoriesRes>RequiredResolves to the list of product categories with pagination fields.
ResponsePromise
ResponsePromise<StoreGetProductCategoriesRes>Requiredretrieve
Retrieve a Product Category's details.
Example
A simple example that retrieves a product category by its ID:
To specify relations that should be retrieved:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.productCategories.retrieve(productCategoryId, {
expand: "products"
})
.then(({ product_category }) => {
console.log(product_category.id);
})
Parameters
id
stringRequiredConfigurations to apply on the retrieved product categories.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<StoreGetProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<StoreGetProductCategoriesCategoryRes>Required