Skip to main content
Skip to main content

ProductVariantService

Constructors

constructor

**new ProductVariantService**(«destructured»)

Parameters

__namedParametersObjectRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
cartRepository_Repository<Cart> & &#123; findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations &#125;Required
eventBus_EventBusServiceRequired
manager_EntityManagerRequired
moneyAmountRepository_Repository<MoneyAmount> & &#123; 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 &#125;Required
priceSelectionStrategy_IPriceSelectionStrategyRequired
productOptionValueRepository_Repository<ProductOptionValue>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
productVariantRepository_Repository<ProductVariant>Required
regionService_RegionServiceRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired

Default: "product-variant.created"

Events.DELETEDstringRequired

Default: "product-variant.deleted"

Events.UPDATEDstringRequired

Default: "product-variant.updated"

Accessors

activeManager_

Protected get**activeManager_**(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Methods

addOptionValue

**addOptionValue**(variantId, optionId, optionValue): Promise&#60;[ProductOptionValue](/references/services/classes/ProductOptionValue)&#62;

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_**<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

create

**create**<TypeParameter TVariants, TypeParameter TOutput>(productOrProductId, variants): Promise&#60;TOutput&#62;

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&#60;void&#62;

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&#60;void&#62;

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&#60;[ProductVariant](/references/services/classes/ProductVariant)&#62;

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&#60;null \| number&#62;

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&#60;boolean&#62;

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&#60;[ProductVariant](/references/services/classes/ProductVariant)[]&#62;

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&#60;[[ProductVariant](/references/services/classes/ProductVariant)[], number]&#62;

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&#60;[ProductVariant](/references/services/classes/ProductVariant)&#62;

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&#60;[ProductVariant](/references/services/classes/ProductVariant)&#62;

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&#60;[MoneyAmount](/references/services/classes/MoneyAmount)&#62;

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&#60;[MoneyAmount](/references/services/classes/MoneyAmount)&#62;

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> | &#123; code: string &#125;Required

Returns

boolean

booleanboolean

update

**update**(variantData): Promise&#60;[ProductVariant](/references/services/classes/ProductVariant)[]&#62;

Updates a collection of variant.

Parameters

variantData&#123; updateData: [UpdateProductVariantInput](/references/services/types/UpdateProductVariantInput) ; variant: [ProductVariant](/references/services/classes/ProductVariant) &#125;[]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&#60;[ProductVariant](/references/services/classes/ProductVariant)&#62;

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&#60;[ProductVariant](/references/services/classes/ProductVariant)&#62;

Parameters

variantOrVariantIdstring | Partial<ProductVariant>Required

Returns

Promise<ProductVariant>

PromisePromise<ProductVariant>Required

updateBatch

Protected **updateBatch**(variantData): Promise&#60;[ProductVariant](/references/services/classes/ProductVariant)[]&#62;

Parameters

variantDataUpdateProductVariantData[]Required

Returns

Promise<ProductVariant[]>

PromisePromise<ProductVariant[]>Required

updateOptionValue

**updateOptionValue**(variantId, optionId, optionValue): Promise&#60;[ProductOptionValue](/references/services/classes/ProductOptionValue)&#62;

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&#60;void&#62;

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&#60;void&#62;

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&#60;void&#62;

Parameters

Returns

Promise<void>

PromisePromise<void>Required

upsertCurrencyPrices

**upsertCurrencyPrices**(data): Promise&#60;void&#62;

Parameters

data&#123; price: [WithRequiredProperty](/references/services/types/WithRequiredProperty)&#60;[ProductVariantPrice](/references/services/types/ProductVariantPrice), "currency_code"&#62; ; variantId: string &#125;[]Required

Returns

Promise<void>

PromisePromise<void>Required

upsertRegionPrices

**upsertRegionPrices**(data): Promise&#60;void&#62;

Parameters

Returns

Promise<void>

PromisePromise<void>Required

validateVariantsToCreate_

Protected **validateVariantsToCreate_**(product, variants): void

Parameters

productProductRequired
A product is a saleable item that holds general information such as name or description. It must include at least one Product Variant, where each product variant defines different options to purchase the product with (for example, different sizes or colors). The prices and inventory of the product are defined on the variant level.
variantsCreateProductVariantInput[]Required

Returns

void

voidvoid

withTransaction

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

Parameters

transactionManagerEntityManager

Returns

ProductVariantService

ProductVariantServiceProductVariantServiceRequired
Was this section helpful?