🚨 Announcing Vendure v2 Beta

ProductPriceApplicator

ProductPriceApplicator

This helper is used to apply the correct price to a ProductVariant based on the current context including active Channel, any current Order, etc. If you use the TransactionalConnection to directly query ProductVariants, you will find that the price and priceWithTax properties will always be 0 until you use the applyChannelPriceAndTax() method:

Example

export class MyCustomService {
  constructor(private connection: TransactionalConnection,
              private productPriceApplicator: ProductPriceApplicator) {}

  getVariant(ctx: RequestContext, id: ID) {
    const productVariant = await this.connection
      .getRepository(ctx, ProductVariant)
      .findOne(id, { relations: ['taxCategory'] });

    await this.productPriceApplicator
      .applyChannelPriceAndTax(productVariant, ctx);

    return productVariant;
  }
}

Signature

class ProductPriceApplicator {
  constructor(configService: ConfigService, taxRateService: TaxRateService, zoneService: ZoneService, requestCache: RequestContextCacheService)
  async applyChannelPriceAndTax(variant: ProductVariant, ctx: RequestContext, order?: Order) => Promise<ProductVariant>;
}

Members

constructor

method
type:
(configService: ConfigService, taxRateService: TaxRateService, zoneService: ZoneService, requestCache: RequestContextCacheService) => ProductPriceApplicator

applyChannelPriceAndTax

async method
type:
(variant: ProductVariant, ctx: RequestContext, order?: Order) => Promise<ProductVariant>
Populates the price field with the price for the specified channel. Make sure that the ProductVariant being passed in has its taxCategory relation joined.