Skip to main content

Class: ProductService

Provides layer to manipulate products.

Hierarchy

  • "medusa-interfaces"

    ProductService

Constructors

constructor

new ProductService(__namedParameters)

Parameters

NameType
__namedParametersObject

Overrides

BaseService.constructor

Defined in

services/product.js:19

Properties

Events

Static Events: Object

Type declaration

NameType
CREATEDstring
DELETEDstring
UPDATEDstring

Defined in

services/product.js:13


IndexName

Static IndexName: string

Defined in

services/product.js:12

Methods

addOption

addOption(productId, optionTitle): Promise<any>

Adds an option to a product. Options can, for example, be "Size", "Color", etc. Will update all the products variants with a dummy value for the newly created option. The same option cannot be added more than once.

Parameters

NameTypeDescription
productIdstringthe product to apply the new option to
optionTitlestringthe display title of the option, e.g. "Size"

Returns

Promise<any>

the result of the model update operation

Defined in

services/product.js:707


count

count(selector?): Promise<any>

Return the total number of documents in database

Parameters

NameTypeDescription
selectoranythe selector to choose products by

Returns

Promise<any>

the result of the count operation

Defined in

services/product.js:223


create

create(productObject): Promise<any>

Creates a product.

Parameters

NameTypeDescription
productObjectanythe product to create

Returns

Promise<any>

resolves to the creation result.

Defined in

services/product.js:483


decorate

decorate(productId, fields?, expandFields?, config?): Product

Decorates a product with product variants.

Parameters

NameTypeDefault valueDescription
productIdstringundefinedthe productId to decorate.
fieldsstring[][]the fields to include.
expandFieldsstring[][]fields to expand.
configany{}retrieve config for price calculation.

Returns

Product

return the decorated product.

Defined in

services/product.js:945


delete

delete(productId): Promise<any>

Deletes a product from a given product id. The product's associated variants will also be deleted.

Parameters

NameTypeDescription
productIdstringthe id of the product to delete. Must be castable as an ObjectId

Returns

Promise<any>

empty promise

Defined in

services/product.js:673


deleteOption

deleteOption(productId, optionId): Promise<any>

Delete an option from a product.

Parameters

NameTypeDescription
productIdstringthe product to delete an option from
optionIdstringthe option to delete

Returns

Promise<any>

the updated product

Defined in

services/product.js:881


getFreeTextQueryBuilder_

getFreeTextQueryBuilder_(productRepo, query, q): QueryBuilder<Product>

Creates a QueryBuilder that can fetch products based on free text.

Parameters

NameTypeDescription
productRepoProductRepositoryan instance of a ProductRepositry
queryFindOptions<Product>the query to get products by
qstringthe text to perform free text search from

Returns

QueryBuilder<Product>

a query builder that can fetch products

Defined in

services/product.js:1014


list

list(selector?, config?): Promise<Product[]>

Lists products based on the provided parameters.

Parameters

NameTypeDescription
selectoranyan object that defines rules to filter products by
configanyobject that defines the scope for what should be returned

Returns

Promise<Product[]>

the result of the find operation

Defined in

services/product.js:109


listAndCount

listAndCount(selector?, config?): Promise<[Product[], number]>

Lists products based on the provided parameters and includes the count of products that match the query.

Parameters

NameTypeDescription
selectoranyan object that defines rules to filter products by
configanyobject that defines the scope for what should be returned

Returns

Promise<[Product[], number]>

an array containing the products as the first element and the total count of products that matches the query as the second element.

Defined in

services/product.js:164


listTagsByUsage

listTagsByUsage(count?): Promise<any>

Parameters

NameTypeDefault value
countnumber10

Returns

Promise<any>

Defined in

services/product.js:412


listTypes

listTypes(): Promise<any>

Returns

Promise<any>

Defined in

services/product.js:404


prepareListQuery_

prepareListQuery_(selector, config): any

Creates a query object to be used for list queries.

Parameters

NameTypeDescription
selectoranythe selector to create the query from
configanythe config to use for the query

Returns

any

an object containing the query, relations and free-text search param.

Defined in

services/product.js:980


reorderOptions

reorderOptions(productId, optionOrder): Promise<any>

Changes the order of a product's options. Will throw if the length of optionOrder and the length of the product's options are different. Will throw optionOrder contains an id not associated with the product.

Parameters

NameTypeDescription
productIdstringthe product whose options we are reordering
optionOrderstring[]the ids of the product's options in the new order

Returns

Promise<any>

the result of the update operation

Defined in

services/product.js:790


reorderVariants

reorderVariants(productId, variantOrder): Promise<any>

Parameters

NameType
productIdany
variantOrderany

Returns

Promise<any>

Defined in

services/product.js:746


retrieve

retrieve(productId, config?): Promise<Product>

Gets a product by id. Throws in case of DB Error and if product was not found.

Parameters

NameTypeDescription
productIdstringid of the product to get.
configanyobject that defines what should be included in the query response

Returns

Promise<Product>

the result of the find one operation.

Defined in

services/product.js:239


retrieveByExternalId

retrieveByExternalId(externalId, config?): Promise<Product>

Gets a product by external id. Throws in case of DB Error and if product was not found.

Parameters

NameTypeDescription
externalIdstringhandle of the product to get.
configanydetails about what to get from the product

Returns

Promise<Product>

the result of the find one operation.

Defined in

services/product.js:342


retrieveByHandle

retrieveByHandle(productHandle, config?): Promise<Product>

Gets a product by handle. Throws in case of DB Error and if product was not found.

Parameters

NameTypeDescription
productHandlestringhandle of the product to get.
configanydetails about what to get from the product

Returns

Promise<Product>

the result of the find one operation.

Defined in

services/product.js:291


retrieveVariants

retrieveVariants(productId, config?): Promise<any>

Gets all variants belonging to a product.

Parameters

NameTypeDescription
productIdstringthe id of the product to get variants from.
configFindConfig<Product>The config to select and configure relations etc...

Returns

Promise<any>

an array of variants

Defined in

services/product.js:392


setAdditionalPrices

setAdditionalPrices(products, currency_code, region_id, cart_id, customer_id, include_discount_prices?): Promise<Product[]>

Set additional prices on a list of products.

Parameters

NameTypeDefault valueDescription
productsanyundefinedlist of products on which to set additional prices
currency_codestringundefinedcurrency code to fetch prices for
region_idstringundefinedregion to fetch prices for
cart_idstringundefinedstring of cart to use as a basis for getting currency and region
customer_idstringundefinedid of potentially logged in customer, used to get prices valid for their customer groups
include_discount_pricesbooleanfalseindication wether or not to include sales prices in result

Returns

Promise<Product[]>

A list of products with variants decorated with "additional_prices"

Defined in

services/product.js:1055


update

update(productId, update): Promise<any>

Updates a product. Product variant updates should use dedicated methods, e.g. addVariant, etc. The function will throw errors if metadata or product variant updates are attempted.

Parameters

NameTypeDescription
productIdstringthe id of the product. Must be a string that can be casted to an ObjectId
updateanyan object with the update values.

Returns

Promise<any>

resolves to the update result.

Defined in

services/product.js:573


updateOption

updateOption(productId, optionId, data): Promise<any>

Updates a product's option. Throws if the call tries to update an option not associated with the product. Throws if the updated title already exists.

Parameters

NameTypeDescription
productIdstringthe product whose option we are updating
optionIdstringthe id of the option we are updating
dataanythe data to update the option with

Returns

Promise<any>

the updated product

Defined in

services/product.js:831


upsertImages_

upsertImages_(images): Promise<any[]>

Parameters

NameType
imagesany

Returns

Promise<any[]>

Defined in

services/product.js:542


upsertProductTags_

upsertProductTags_(tags): Promise<any[]>

Parameters

NameType
tagsany

Returns

Promise<any[]>

Defined in

services/product.js:455


upsertProductType_

upsertProductType_(type): Promise<any>

Parameters

NameType
typeany

Returns

Promise<any>

Defined in

services/product.js:430


withTransaction

withTransaction(transactionManager): ProductService

Parameters

NameType
transactionManagerany

Returns

ProductService

Defined in

services/product.js:76