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 user 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
addProducts
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
Resolves to the product category's details.
create
Create a product category.
Example
Parameters
The product category's details.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product category's details.
delete
Delete a product category. This does not delete associated products.
Example
Parameters
productCategoryIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the deletion operation's details.
list
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
Filters and pagination configurations to apply on the retrieved product categories.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the list of product categories with pagination fields.
removeProducts
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
Resolves to the product category's details.
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
productCategoryIdstringRequiredConfigurations to apply on the retrieved product category.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product category's details.
update
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: {}