Skip to main content
Skip to main content

ProductVariantService

internal.internal.ProductVariantService

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
cartRepository_Repository<Cart> & { findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations }Required
eventBus_EventBusServiceRequired
manager_EntityManagerRequired
moneyAmountRepository_Repository<MoneyAmount> & { addPriceListPrices: Method addPriceListPrices ; createProductVariantMoneyAmounts: Method createProductVariantMoneyAmounts ; deletePriceListPrices: Method deletePriceListPrices ; deleteVariantPricesNotIn: Method deleteVariantPricesNotIn ; findCurrencyMoneyAmounts: Method findCurrencyMoneyAmounts ; findManyForVariantInPriceList: Method findManyForVariantInPriceList ; findManyForVariantInRegion: Method findManyForVariantInRegion ; findManyForVariantsInRegion: Method findManyForVariantsInRegion ; findRegionMoneyAmounts: Method findRegionMoneyAmounts ; findVariantPricesNotIn: Method findVariantPricesNotIn ; getPricesForVariantInRegion: Method getPricesForVariantInRegion ; insertBulk: Method insertBulk ; updatePriceListPrices: Method updatePriceListPrices ; upsertVariantCurrencyPrice: Method upsertVariantCurrencyPrice }Required
priceSelectionStrategy_IPriceSelectionStrategyRequired
productOptionValueRepository_Repository<ProductOptionValue>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
productVariantRepository_Repository<ProductVariant>Required
regionService_RegionServiceRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired
Events.DELETEDstringRequired
Events.UPDATEDstringRequired

Accessors

activeManager_

Protected get activeManager_(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Inherited from

TransactionBaseService.activeManager_

Methods

addOptionValue

addOptionValue(variantId, optionId, optionValue): Promise<ProductOptionValue>

Adds option value to a variant. Fails when product with variant does not exist or if that product does not have an option with the given option id. Fails if given variant is not found. Option value must be of type string or number.

Parameters

variantIdstringRequired
the variant to decorate.
optionIdstringRequired
the option from product.
optionValuestringRequired
option value to add.

Returns

Promise<ProductOptionValue>

PromisePromise<ProductOptionValue>Required
the result of the 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_


create

create<TVariants, TOutput>(productOrProductId, variants): Promise<TOutput>

Creates an unpublished product variant. Will validate against parent product to ensure that the variant can in fact be created.

TOutputobjectRequired

Parameters

productOrProductIdstring | ProductRequired
the product the variant will be added to

Returns

Promise<TOutput>

PromisePromise<TOutput>Required
resolves to the creation result.

delete

delete(variantIds): Promise<void>

Deletes variant or variants. Will never fail due to delete being idempotent.

Parameters

variantIdsstring | string[]Required
the id of the variant to delete. Must be castable as an ObjectId

Returns

Promise<void>

PromisePromise<void>Required
empty promise

deleteOptionValue

deleteOptionValue(variantId, optionId): Promise<void>

Deletes option value from given variant. Will never fail due to delete being idempotent.

Parameters

variantIdstringRequired
the variant to decorate.
optionIdstringRequired
the option from product.

Returns

Promise<void>

PromisePromise<void>Required
empty promise

getFreeTextQueryBuilder_

getFreeTextQueryBuilder_(variantRepo, query, q?): SelectQueryBuilder<ProductVariant>

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

Parameters

variantRepoRepository<ProductVariant>Required
the variant repository
object that defines the scope for what should be returned
qstring
free text query

Returns

SelectQueryBuilder<ProductVariant>

SelectQueryBuilderSelectQueryBuilder<ProductVariant>Required
an array containing the products as the first element and the total count of products that matches the query as the second element.

getRegionPrice

getRegionPrice(variantId, context): Promise<null | number>

Gets the price specific to a region. If no region specific money amount exists the function will try to use a currency price. If no default currency price exists the function will throw an error.

Parameters

variantIdstringRequired
the id of the variant to get price from
contextGetRegionPriceContextRequired
context for getting region price

Returns

Promise<null | number>

PromisePromise<null | number>Required
the price specific to the region

isVariantInSalesChannels

isVariantInSalesChannels(id, salesChannelIds): Promise<boolean>

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

Parameters

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

Returns

Promise<boolean>

PromisePromise<boolean>Required

list

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

Parameters

the query object for find
query config object for variant retrieval

Returns

Promise<ProductVariant[]>

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

listAndCount

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

Parameters

the query object for find
query config object for variant retrieval

Returns

Promise<[ProductVariant[], number]>

PromisePromise<[ProductVariant[], number]>Required
the result of the find operation

retrieve

retrieve(variantId, config?): Promise<ProductVariant>

Gets a product variant by id.

Parameters

variantIdstringRequired
the id of the product to get.
query config object for variant retrieval.

Returns

Promise<ProductVariant>

PromisePromise<ProductVariant>Required
the product document.

retrieveBySKU

retrieveBySKU(sku, config?): Promise<ProductVariant>

Gets a product variant by id.

Parameters

skustringRequired
The unique stock keeping unit used to identify the product variant.
query config object for variant retrieval.

Returns

Promise<ProductVariant>

PromisePromise<ProductVariant>Required
the product document.

setCurrencyPrice

setCurrencyPrice(variantId, price): Promise<MoneyAmount>

Parameters

variantIdstringRequired
the id of the variant to set prices for
priceProductVariantPriceRequired
the price for the variant

Returns

Promise<MoneyAmount>

PromisePromise<MoneyAmount>Required
the result of the update operation

Deprecated

use addOrUpdateCurrencyPrices instead Sets the default price for the given currency.


setRegionPrice

setRegionPrice(variantId, price): Promise<MoneyAmount>

Parameters

variantIdstringRequired
the id of the variant to update
priceProductVariantPriceRequired
the price for the variant.

Returns

Promise<MoneyAmount>

PromisePromise<MoneyAmount>Required
the result of the update operation

Deprecated

use addOrUpdateRegionPrices instead Sets the default price of a specific region


shouldRetryTransaction_

Protected shouldRetryTransaction_(err): boolean

Parameters

errRecord<string, unknown> | { code: string }Required

Returns

boolean

booleanboolean

Inherited from

TransactionBaseService.shouldRetryTransaction_


update

update(variantData): Promise<ProductVariant[]>

Updates a collection of variant.

Parameters

variantData{ updateData: UpdateProductVariantInput ; variant: ProductVariant }[]Required
a collection of variant and the data to update.

Returns

Promise<ProductVariant[]>

PromisePromise<ProductVariant[]>Required
resolves to the update result.

update(variantOrVariantId, update): Promise<ProductVariant>

Updates a variant. Price updates should use dedicated methods. The function will throw, if price updates are attempted.

Parameters

variantOrVariantIdstring | Partial<ProductVariant>Required
variant or id of a variant.
an object with the update values.

Returns

Promise<ProductVariant>

PromisePromise<ProductVariant>Required
resolves to the update result.

update(variantOrVariantId, update): Promise<ProductVariant>

Parameters

variantOrVariantIdstring | Partial<ProductVariant>Required

Returns

Promise<ProductVariant>

PromisePromise<ProductVariant>Required

updateBatch

Protected updateBatch(variantData): Promise<ProductVariant[]>

Parameters

variantDataUpdateProductVariantData[]Required

Returns

Promise<ProductVariant[]>

PromisePromise<ProductVariant[]>Required

updateOptionValue

updateOptionValue(variantId, optionId, optionValue): Promise<ProductOptionValue>

Updates variant's option value. Option value must be of type string or number.

Parameters

variantIdstringRequired
the variant to decorate.
optionIdstringRequired
the option from product.
optionValuestringRequired
option value to add.

Returns

Promise<ProductOptionValue>

PromisePromise<ProductOptionValue>Required
the result of the update operation.

updateVariantPrices

updateVariantPrices(data): Promise<void>

Updates variant/prices collection. Deletes any prices that are not in the update object, and is not associated with a price list.

Parameters

Returns

Promise<void>

PromisePromise<void>Required
empty promise

updateVariantPrices(variantId, prices): Promise<void>

Updates a variant's prices. Deletes any prices that are not in the update object, and is not associated with a price list.

Parameters

variantIdstringRequired
the id of variant
pricesProductVariantPrice[]Required
the update prices

Returns

Promise<void>

PromisePromise<void>Required
empty promise

updateVariantPricesBatch

Protected updateVariantPricesBatch(data): Promise<void>

Parameters

Returns

Promise<void>

PromisePromise<void>Required

upsertCurrencyPrices

upsertCurrencyPrices(data): Promise<void>

Parameters

data{ price: WithRequiredProperty<ProductVariantPrice, "currency_code"> ; variantId: string }[]Required

Returns

Promise<void>

PromisePromise<void>Required

upsertRegionPrices

upsertRegionPrices(data): Promise<void>

Parameters

Returns

Promise<void>

PromisePromise<void>Required

validateVariantsToCreate_

Protected validateVariantsToCreate_(product, variants): void

Parameters

productProductRequired
variantsCreateProductVariantInput[]Required

Returns

void

voidvoid

withTransaction

withTransaction(transactionManager?): ProductVariantService

Parameters

transactionManagerEntityManager

Returns

ProductVariantService

ProductVariantServiceProductVariantServiceRequired

Inherited from

TransactionBaseService.withTransaction

Was this section helpful?