Skip to main content
Skip to main content

ProductService

Constructors

constructor

**new ProductService**(«destructured»)

Parameters

__namedParametersInjectedDependenciesRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
imageRepository_Repository<Image> & &#123; insertBulk: Method insertBulk ; upsertImages: Method upsertImages &#125;Required
manager_EntityManagerRequired
productCategoryRepository_TreeRepository<ProductCategory> & &#123; addProducts: Method addProducts ; findOneWithDescendants: Method findOneWithDescendants ; getFreeTextSearchResultsAndCount: Method getFreeTextSearchResultsAndCount ; removeProducts: Method removeProducts &#125;Required
productOptionRepository_Repository<ProductOption>Required
productRepository_Repository<Product> & &#123; _applyCategoriesQuery: Method \_applyCategoriesQuery ; _findWithRelations: Method \_findWithRelations ; bulkAddToCollection: Method bulkAddToCollection ; bulkRemoveFromCollection: Method bulkRemoveFromCollection ; findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations ; findWithRelationsAndCount: Method findWithRelationsAndCount ; getCategoryIdsFromInput: Method getCategoryIdsFromInput ; getCategoryIdsRecursively: Method getCategoryIdsRecursively ; getFreeTextSearchResultsAndCount: Method getFreeTextSearchResultsAndCount ; isProductInSalesChannels: Method isProductInSalesChannels ; queryProducts: Method queryProducts ; queryProductsWithIds: Method queryProductsWithIds &#125;Required
productTagRepository_Repository<ProductTag> & &#123; findAndCountByDiscountConditionId: Method findAndCountByDiscountConditionId ; insertBulk: Method insertBulk ; listTagsByUsage: Method listTagsByUsage ; upsertTags: Method upsertTags &#125;Required
productTypeRepository_Repository<ProductType> & &#123; findAndCountByDiscountConditionId: Method findAndCountByDiscountConditionId ; upsertType: Method upsertType &#125;Required
productVariantRepository_Repository<ProductVariant>Required
productVariantService_ProductVariantServiceRequired
searchService_SearchServiceRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired

Default: "product.created"

Events.DELETEDstringRequired

Default: "product.deleted"

Events.UPDATEDstringRequired

Default: "product.updated"

IndexName"products"Required

Accessors

activeManager_

Protected get**activeManager_**(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Methods

addOption

**addOption**(productId, optionTitle): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

productIdstringRequired
the product to apply the new option to
optionTitlestringRequired
the display title of the option, e.g. "Size"

Returns

Promise<Product>

PromisePromise<Product>Required
the result of the model update operation

atomicPhase_

Protected **atomicPhase_**<TypeParameter TResult, TypeParameter TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise&#60;TResult&#62;

Wraps some work within a transactional block. If the service already has a transaction manager attached this will be reused, otherwise a new transaction manager is created.

TResultobjectRequired
TErrorobjectRequired

Parameters

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns

Promise<TResult>

PromisePromise<TResult>Required
the result of the transactional work

count

**count**(selector?): Promise&#60;number&#62;

Return the total number of documents in database

Parameters

selectorSelector<Product>Required
the selector to choose products by

Default: {}

Returns

Promise<number>

PromisePromise<number>Required
the result of the count operation

create

**create**(productObject): Promise&#60;[Product](/references/services/classes/Product)&#62;

Creates a product.

Parameters

productObjectCreateProductInputRequired
the product to create

Returns

Promise<Product>

PromisePromise<Product>Required
resolves to the creation result.

delete

**delete**(productId): Promise&#60;void&#62;

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

Parameters

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

Returns

Promise<void>

PromisePromise<void>Required
empty promise

deleteOption

**deleteOption**(productId, optionId): Promise&#60;void \| [Product](/references/services/classes/Product)&#62;

Delete an option from a product.

Parameters

productIdstringRequired
the product to delete an option from
optionIdstringRequired
the option to delete

Returns

Promise<void | Product>

PromisePromise<void | Product>Required
the updated product

filterProductsBySalesChannel

**filterProductsBySalesChannel**(productIds, salesChannelId, config?): Promise&#60;[Product](/references/services/classes/Product)[]&#62;

Parameters

productIdsstring[]Required
salesChannelIdstringRequired
configFindProductConfigRequired

Returns

Promise<Product[]>

PromisePromise<Product[]>Required

isProductInSalesChannels

**isProductInSalesChannels**(id, salesChannelIds): Promise&#60;boolean&#62;

Check if the product is assigned to at least one of the provided sales channels.

Parameters

idstringRequired
product id
salesChannelIdsstring[]Required
an array of sales channel ids

Returns

Promise<boolean>

PromisePromise<boolean>Required

list

**list**(selector, config?): Promise&#60;[Product](/references/services/classes/Product)[]&#62;

Lists products based on the provided parameters.

Parameters

selectorProductSelectorRequired
an object that defines rules to filter products by
configFindProductConfigRequired
object that defines the scope for what should be returned

Returns

Promise<Product[]>

PromisePromise<Product[]>Required
the result of the find operation

listAndCount

**listAndCount**(selector, config?): Promise&#60;[[Product](/references/services/classes/Product)[], number]&#62;

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

Parameters

selectorProductSelectorRequired
an object that defines rules to filter products by
configFindProductConfigRequired
object that defines the scope for what should be returned

Returns

Promise<[Product[], number]>

PromisePromise<[Product[], number]>Required
an array containing the products as the first element and the total count of products that matches the query as the second element.

listTagsByUsage

**listTagsByUsage**(take?): Promise&#60;[ProductTag](/references/services/classes/ProductTag)[]&#62;

Parameters

takenumberRequired

Default: 10

Returns

Promise<ProductTag[]>

PromisePromise<ProductTag[]>Required

listTypes

**listTypes**(): Promise&#60;[ProductType](/references/services/classes/ProductType)[]&#62;

Returns

Promise<ProductType[]>

PromisePromise<ProductType[]>Required

prepareListQuery_

Protected **prepareListQuery_**(selector, config): &#123; q: string ; query: [FindWithoutRelationsOptions](/references/services/types/FindWithoutRelationsOptions-1) ; relations: keyof [Product](/references/services/classes/Product)[] &#125;

Temporary method to be used in place we need custom query strategy to prevent typeorm bug

Parameters

Returns

object

objectobject
qstringRequired
relationskeyof Product[]Required

reorderVariants

**reorderVariants**(productId, variantOrder): Promise&#60;[Product](/references/services/classes/Product)&#62;

Parameters

productIdstringRequired
variantOrderstring[]Required

Returns

Promise<Product>

PromisePromise<Product>Required

retrieve

**retrieve**(productId, config?): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

Parameters

productIdstringRequired
id of the product to get.
configFindProductConfigRequired
object that defines what should be included in the query response

Returns

Promise<Product>

PromisePromise<Product>Required
the result of the find one operation.

retrieveByExternalId

**retrieveByExternalId**(externalId, config?): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

Parameters

externalIdstringRequired
handle of the product to get.
configFindProductConfigRequired
details about what to get from the product

Default: {}

Returns

Promise<Product>

PromisePromise<Product>Required
the result of the find one operation.

retrieveByHandle

**retrieveByHandle**(productHandle, config?): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

Parameters

productHandlestringRequired
handle of the product to get.
configFindProductConfigRequired
details about what to get from the product

Default: {}

Returns

Promise<Product>

PromisePromise<Product>Required
the result of the find one operation.

retrieveOptionByTitle

**retrieveOptionByTitle**(title, productId): Promise&#60;null \| [ProductOption](/references/services/classes/ProductOption)&#62;

Retrieve product's option by title.

Parameters

titlestringRequired
title of the option
productIdstringRequired
id of a product

Returns

Promise<null | ProductOption>

PromisePromise<null | ProductOption>Required
product option

retrieveVariants

**retrieveVariants**(productId, config?): Promise&#60;[ProductVariant](/references/services/classes/ProductVariant)[]&#62;

Gets all variants belonging to a product.

Parameters

productIdstringRequired
the id of the product to get variants from.
configFindProductConfigRequired
The config to select and configure relations etc...

Returns

Promise<ProductVariant[]>

PromisePromise<ProductVariant[]>Required
an array of variants

retrieve_

**retrieve_**(selector, config?): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

Parameters

selectorSelector<Product>Required
selector object
configFindProductConfigRequired
object that defines what should be included in the query response

Returns

Promise<Product>

PromisePromise<Product>Required
the result of the find one operation.

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters

errRecord<string, unknown> | &#123; code: string &#125;Required

Returns

boolean

booleanboolean

update

**update**(productId, update): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

productIdstringRequired
the id of the product. Must be a string that can be casted to an ObjectId
updateUpdateProductInputRequired
an object with the update values.

Returns

Promise<Product>

PromisePromise<Product>Required
resolves to the update result.

updateOption

**updateOption**(productId, optionId, data): Promise&#60;[Product](/references/services/classes/Product)&#62;

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

productIdstringRequired
the product whose option we are updating
optionIdstringRequired
the id of the option we are updating
dataProductOptionInputRequired
the data to update the option with

Returns

Promise<Product>

PromisePromise<Product>Required
the updated product

updateShippingProfile

**updateShippingProfile**(productIds, profileId): Promise&#60;[Product](/references/services/classes/Product)[]&#62;

Assign a product to a profile, if a profile id null is provided then detach the product from the profile

Parameters

productIdsstring | string[]Required
ID or IDs of the products to update
profileIdnull | stringRequired
Shipping profile ID to update the shipping options with

Returns

Promise<Product[]>

PromisePromise<Product[]>Required
updated products

withTransaction

**withTransaction**(transactionManager?): [ProductService](/references/services/classes/ProductService)

Parameters

transactionManagerEntityManager

Returns

ProductService

ProductServiceProductServiceRequired
Was this section helpful?