Skip to main content
Skip to main content

AdminProductCategoriesResource

Note

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:

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)
.then(({ product_category }) => {
console.log(product_category.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

productCategoryIdstringRequired
The ID of the product category.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Configurations to apply on the retrieved product category.

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required
Resolves to the product category's details.

create

Create 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.create({
name: "Skinny Jeans",
})
.then(({ product_category }) => {
console.log(product_category.id);
})

Parameters

The product category's details.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required
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

productCategoryIdstringRequired
The ID of the product category.
The attributes to update in the product category.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required
Resolves to the product category'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:

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()
.then(({ product_categories, limit, offset, count }) => {
console.log(product_categories.length);
})

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>Required
Custom headers to attach to the request.

Default: {}

Filters and pagination configurations to apply on the retrieved product categories.

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesListRes>Required
Resolves to the list of product categories with pagination fields.

delete

Delete a product category. This does not delete associated products.

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.delete(productCategoryId)
.then(({ id, object, deleted }) => {
console.log(id);
})

Parameters

productCategoryIdstringRequired
The ID of the product category.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<DeleteResponse>Required
Resolves to the deletion operation's details.

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

productCategoryIdstringRequired
The ID of the product category.
The products to delete.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required
Resolves to the product category's details.

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

productCategoryIdstringRequired
The ID of the product category.
The products to add.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminProductCategoriesCategoryRes>Required
Resolves to the product category's details.
Was this section helpful?