Skip to main content
Skip to main content

ProductService

internal.internal.ProductService

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
imageRepository_Repository<Image> & { insertBulk: Method insertBulk ; upsertImages: Method upsertImages }Required
manager_EntityManagerRequired
productCategoryRepository_TreeRepository<ProductCategory> & { addProducts: Method addProducts ; findOneWithDescendants: Method findOneWithDescendants ; getFreeTextSearchResultsAndCount: Method getFreeTextSearchResultsAndCount ; removeProducts: Method removeProducts }Required
productOptionRepository_Repository<ProductOption>Required
productRepository_Repository<Product> & { _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 }Required
productTagRepository_Repository<ProductTag> & { findAndCountByDiscountConditionId: Method findAndCountByDiscountConditionId ; insertBulk: Method insertBulk ; listTagsByUsage: Method listTagsByUsage ; upsertTags: Method upsertTags }Required
productTypeRepository_Repository<ProductType> & { findAndCountByDiscountConditionId: Method findAndCountByDiscountConditionId ; upsertType: Method upsertType }Required
productVariantRepository_Repository<ProductVariant>Required
productVariantService_ProductVariantServiceRequired
searchService_SearchServiceRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired
Events.DELETEDstringRequired
Events.UPDATEDstringRequired
IndexName"products"Required

Default: `"products"`

Accessors

activeManager_

Protected get activeManager_(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Inherited from

TransactionBaseService.activeManager_

Methods

addOption

addOption(productId, optionTitle): Promise<Product>

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_<TResult, TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise<TResult>

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

Inherited from

TransactionBaseService.atomicPhase_


count

count(selector?): Promise<number>

Return the total number of documents in database

Parameters

selectorSelector<Product>
the selector to choose products by

Returns

Promise<number>

PromisePromise<number>Required
the result of the count operation

create

create(productObject): Promise<Product>

Creates a product.

Parameters

productObjectCreateProductInputRequired
the product to create

Returns

Promise<Product>

PromisePromise<Product>Required
resolves to the creation result.

delete

delete(productId): Promise<void>

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<void | Product>

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<Product[]>

Parameters

productIdsstring[]Required
salesChannelIdstringRequired

Returns

Promise<Product[]>

PromisePromise<Product[]>Required

isProductInSalesChannels

isProductInSalesChannels(id, salesChannelIds): Promise<boolean>

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<Product[]>

Lists products based on the provided parameters.

Parameters

selectorProductSelectorRequired
an object that defines rules to filter products by
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<[Product[], number]>

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
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<ProductTag[]>

Parameters

takenumber

Returns

Promise<ProductTag[]>

PromisePromise<ProductTag[]>Required

listTypes

listTypes(): Promise<ProductType[]>

Returns

Promise<ProductType[]>

PromisePromise<ProductType[]>Required

prepareListQuery_

Protected prepareListQuery_(selector, config): { q: string ; query: FindWithoutRelationsOptions ; relations: keyof Product[] }

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

Parameters

Returns

object

`object`object
qstringRequired
relationskeyof Product[]Required

reorderVariants

reorderVariants(productId, variantOrder): Promise<Product>

Parameters

productIdstringRequired
variantOrderstring[]Required

Returns

Promise<Product>

PromisePromise<Product>Required

retrieve

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

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.
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<Product>

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.
details about what to get from the product

Returns

Promise<Product>

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

retrieveByHandle

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

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.
details about what to get from the product

Returns

Promise<Product>

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

retrieveOptionByTitle

retrieveOptionByTitle(title, productId): Promise<null | ProductOption>

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<ProductVariant[]>

Gets all variants belonging to a product.

Parameters

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

Returns

Promise<ProductVariant[]>

PromisePromise<ProductVariant[]>Required
an array of variants

retrieve_

retrieve_(selector, config?): Promise<Product>

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

Parameters

selectorSelector<Product>Required
selector object
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> | { code: string }Required

Returns

boolean

booleanboolean

Inherited from

TransactionBaseService.shouldRetryTransaction_


update

update(productId, update): Promise<Product>

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<Product>

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<Product[]>

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

Parameters

transactionManagerEntityManager

Returns

ProductService

ProductServiceProductServiceRequired

Inherited from

TransactionBaseService.withTransaction

Was this section helpful?