Class: ProductService
Provides layer to manipulate products.
Hierarchy
"medusa-interfaces"
↳
ProductService
Constructors
constructor
• new ProductService(__namedParameters
)
Parameters
Name | Type |
---|---|
__namedParameters | Object |
Overrides
BaseService.constructor
Defined in
Properties
Events
▪ Static
Events: Object
Type declaration
Name | Type |
---|---|
CREATED | string |
DELETED | string |
UPDATED | string |
Defined in
IndexName
▪ Static
IndexName: string
Defined in
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
Name | Type | Description |
---|---|---|
productId | string | the product to apply the new option to |
optionTitle | string | the display title of the option, e.g. "Size" |
Returns
Promise
<any
>
the result of the model update operation
Defined in
count
▸ count(selector?
): Promise
<any
>
Return the total number of documents in database
Parameters
Name | Type | Description |
---|---|---|
selector | any | the selector to choose products by |
Returns
Promise
<any
>
the result of the count operation
Defined in
create
▸ create(productObject
): Promise
<any
>
Creates a product.
Parameters
Name | Type | Description |
---|---|---|
productObject | any | the product to create |
Returns
Promise
<any
>
resolves to the creation result.
Defined in
decorate
▸ decorate(productId
, fields?
, expandFields?
, config?
): Product
Decorates a product with product variants.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
productId | string | undefined | the productId to decorate. |
fields | string [] | [] | the fields to include. |
expandFields | string [] | [] | fields to expand. |
config | any | {} | retrieve config for price calculation. |
Returns
Product
return the decorated product.
Defined in
delete
▸ delete(productId
): Promise
<any
>
Deletes a product from a given product id. The product's associated variants will also be deleted.
Parameters
Name | Type | Description |
---|---|---|
productId | string | the id of the product to delete. Must be castable as an ObjectId |
Returns
Promise
<any
>
empty promise
Defined in
deleteOption
▸ deleteOption(productId
, optionId
): Promise
<any
>
Delete an option from a product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | the product to delete an option from |
optionId | string | the option to delete |
Returns
Promise
<any
>
the updated product
Defined in
getFreeTextQueryBuilder_
▸ getFreeTextQueryBuilder_(productRepo
, query
, q
): QueryBuilder
<Product
>
Creates a QueryBuilder that can fetch products based on free text.
Parameters
Name | Type | Description |
---|---|---|
productRepo | ProductRepository | an instance of a ProductRepositry |
query | FindOptions <Product > | the query to get products by |
q | string | the text to perform free text search from |
Returns
QueryBuilder
<Product
>
a query builder that can fetch products
Defined in
list
▸ list(selector?
, config?
): Promise
<Product
[]>
Lists products based on the provided parameters.
Parameters
Name | Type | Description |
---|---|---|
selector | any | an object that defines rules to filter products by |
config | any | object that defines the scope for what should be returned |
Returns
Promise
<Product
[]>
the result of the find operation
Defined in
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
Name | Type | Description |
---|---|---|
selector | any | an object that defines rules to filter products by |
config | any | object 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
listTagsByUsage
▸ listTagsByUsage(count?
): Promise
<any
>
Parameters
Name | Type | Default value |
---|---|---|
count | number | 10 |
Returns
Promise
<any
>
Defined in
listTypes
▸ listTypes(): Promise
<any
>
Returns
Promise
<any
>
Defined in
prepareListQuery_
▸ prepareListQuery_(selector
, config
): any
Creates a query object to be used for list queries.
Parameters
Name | Type | Description |
---|---|---|
selector | any | the selector to create the query from |
config | any | the config to use for the query |
Returns
any
an object containing the query, relations and free-text search param.
Defined in
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
Name | Type | Description |
---|---|---|
productId | string | the product whose options we are reordering |
optionOrder | string [] | the ids of the product's options in the new order |
Returns
Promise
<any
>
the result of the update operation
Defined in
reorderVariants
▸ reorderVariants(productId
, variantOrder
): Promise
<any
>
Parameters
Name | Type |
---|---|
productId | any |
variantOrder | any |
Returns
Promise
<any
>
Defined in
retrieve
▸ retrieve(productId
, config?
): Promise
<Product
>
Gets a product by id. Throws in case of DB Error and if product was not found.
Parameters
Name | Type | Description |
---|---|---|
productId | string | id of the product to get. |
config | any | object that defines what should be included in the query response |
Returns
Promise
<Product
>
the result of the find one operation.
Defined in
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
Name | Type | Description |
---|---|---|
externalId | string | handle of the product to get. |
config | any | details about what to get from the product |
Returns
Promise
<Product
>
the result of the find one operation.
Defined in
retrieveByHandle
▸ retrieveByHandle(productHandle
, config?
): Promise
<Product
>
Gets a product by handle. Throws in case of DB Error and if product was not found.
Parameters
Name | Type | Description |
---|---|---|
productHandle | string | handle of the product to get. |
config | any | details about what to get from the product |
Returns
Promise
<Product
>
the result of the find one operation.
Defined in
retrieveVariants
▸ retrieveVariants(productId
, config?
): Promise
<any
>
Gets all variants belonging to a product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | the id of the product to get variants from. |
config | FindConfig <Product > | The config to select and configure relations etc... |
Returns
Promise
<any
>
an array of variants
Defined in
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
Name | Type | Default value | Description |
---|---|---|---|
products | any | undefined | list of products on which to set additional prices |
currency_code | string | undefined | currency code to fetch prices for |
region_id | string | undefined | region to fetch prices for |
cart_id | string | undefined | string of cart to use as a basis for getting currency and region |
customer_id | string | undefined | id of potentially logged in customer, used to get prices valid for their customer groups |
include_discount_prices | boolean | false | indication wether or not to include sales prices in result |
Returns
Promise
<Product
[]>
A list of products with variants decorated with "additional_prices"
Defined in
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
Name | Type | Description |
---|---|---|
productId | string | the id of the product. Must be a string that can be casted to an ObjectId |
update | any | an object with the update values. |
Returns
Promise
<any
>
resolves to the update result.
Defined in
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
Name | Type | Description |
---|---|---|
productId | string | the product whose option we are updating |
optionId | string | the id of the option we are updating |
data | any | the data to update the option with |
Returns
Promise
<any
>
the updated product
Defined in
upsertImages_
▸ upsertImages_(images
): Promise
<any
[]>
Parameters
Name | Type |
---|---|
images | any |
Returns
Promise
<any
[]>
Defined in
upsertProductTags_
▸ upsertProductTags_(tags
): Promise
<any
[]>
Parameters
Name | Type |
---|---|
tags | any |
Returns
Promise
<any
[]>
Defined in
upsertProductType_
▸ upsertProductType_(type
): Promise
<any
>
Parameters
Name | Type |
---|---|
type | any |
Returns
Promise
<any
>
Defined in
withTransaction
▸ withTransaction(transactionManager
): ProductService
Parameters
Name | Type |
---|---|
transactionManager | any |