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
productCategoryIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Configurations to apply on the retrieved product category.
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Requiredcreate
Create a product category.
Example
Parameters
The product category's details.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromiseResponsePromise<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
productCategoryIdstringRequiredThe attributes to update in the product category.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromiseResponsePromise<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
customHeadersRecord<string, any>RequiredDefault: {}
Filters and pagination configurations to apply on the retrieved product categories.
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesListRes>RequiredResolves to the list of product categories with pagination fields.
ResponsePromiseResponsePromise<AdminProductCategoriesListRes>Requireddelete
Delete a product category. This does not delete associated products.
Example
Parameters
productCategoryIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<DeleteResponse>RequiredResolves to the deletion operation's details.
ResponsePromiseResponsePromise<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
productCategoryIdstringRequiredThe products to delete.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromiseResponsePromise<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
productCategoryIdstringRequiredThe products to add.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>RequiredResolves to the product category's details.
ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required