AdminProductCategoriesResource
To use this resource, make sure to enable its feature flag: product_categories
This class is used to send requests to Admin Product Category API Routes. All its method
are available in the JS Client under the medusa.admin.productCategories
property.
All methods in this class require authentication.
Products can be categoriezed into categories. A product can be added into more than one category.
Related Guide: How to manage product categories.
Methods
retrieve
Retrieve a product category's details.
Example
A simple example that retrieves an order 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.admin.productCategories.retrieve(productCategoryId, {
expand: "category_children"
})
.then(({ product_category }) => {
console.log(product_category.id);
})
Parameters
productCategoryId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Configurations to apply on the retrieved product category.
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>Requiredcreate
Create a product category.
Example
Parameters
The product category's details.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>Requiredupdate
Updates a product category.
Example
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.admin.productCategories.update(productCategoryId, {
name: "Skinny Jeans"
})
.then(({ product_category }) => {
console.log(product_category.id);
})
Parameters
productCategoryId
stringRequiredThe attributes to update in the product category.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>Requiredlist
Retrieve a list of product categories. The product categories can be filtered by fields such as q
or handle
passed in the query
parameter.
The product categories can also be paginated.
Example
To list product categories:
To specify relations that should be retrieved within the product category:
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.admin.productCategories.list({
expand: "category_children"
})
.then(({ product_categories, limit, offset, count }) => {
console.log(product_categories.length);
})
By default, only the first 100
records are retrieved. You can control pagination by specifying the limit
and offset
properties:
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.admin.productCategories.list({
expand: "category_children",
limit,
offset
})
.then(({ product_categories, limit, offset, count }) => {
console.log(product_categories.length);
})
Parameters
customHeaders
Record<string, any>RequiredDefault: {}
Filters and pagination configurations to apply on the retrieved product categories.
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesListRes>RequiredResolves to the list of product categories with pagination fields.
ResponsePromise
ResponsePromise<AdminProductCategoriesListRes>Requireddelete
Delete a product category. This does not delete associated products.
Example
Parameters
productCategoryId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<DeleteResponse>RequiredResolves to the deletion operation's details.
ResponsePromise
ResponsePromise<DeleteResponse>RequiredremoveProducts
Remove a list of products from a product category.
Example
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.admin.productCategories.removeProducts(productCategoryId, {
product_ids: [
{
id: productId
}
]
})
.then(({ product_category }) => {
console.log(product_category.id);
})
Parameters
productCategoryId
stringRequiredThe products to delete.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredaddProducts
Add a list of products to a product category.
Example
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.admin.productCategories.addProducts(productCategoryId, {
product_ids: [
{
id: productId
}
]
})
.then(({ product_category }) => {
console.log(product_category.id);
})
Parameters
productCategoryId
stringRequiredThe products to add.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromise
ResponsePromise<AdminProductCategoriesCategoryRes>Required