@haus-tech/campaign-price-plugin
v3.0.0
Published
Campaign price plugin
Readme
name: campaign-price-plugin title: Campaign Price Plugin description: Vendure plugin that adds campaign pricing with an ordinaryPrice field for "was / now" display. version: 2.0.0 tags: [vendure, plugin, campaign, price, sale]
Campaign Price Plugin
The Campaign Price Plugin is a Vendure plugin that lets you run campaigns and sales by setting a campaign price on product variant prices. The plugin exposes helpers to compose campaign pricing into your own price strategy, and adds an ordinaryPrice field in the Shop API so storefronts can show “was X, now Y” pricing.
Functionality
- Campaign price custom field — Add a
campaignPrice(integer) toProductVariantPrice, editable per channel/currency in the Admin UI. - Campaign helper + strategy export — Use
getCampaignPriceForChannel()andCampaignPriceStrategyto compose campaign pricing in your project strategy setup. - Ordinary price in Shop API —
ProductVariant,Product, andSearchResultget anordinaryPricefield so the storefront can display the original price alongside the current (possibly campaign) price.
Use Cases
- Time-limited sales or campaigns with a different price per channel/currency.
- Showing strikethrough “was X” and “now Y” prices on product and search pages.
- Running campaigns without changing the base price data; turn the campaign off by clearing the campaign price.
Installation
To install the Campaign Price Plugin, follow these steps:
Install the plugin package:
yarn add @haus-tech/campaign-price-pluginOr, if using npm:
npm install @haus-tech/campaign-price-pluginAdd the plugin to your Vendure configuration in
vendure-config.ts:import { CampaignPricePlugin } from '@haus-tech/campaign-price-plugin' export const config = { plugins: [CampaignPricePlugin], }Compose your
productVariantPriceCalculationStrategyin your project (recommended when combining campaign price with other strategies like price lists), or use the functiongetCampaignPriceForChannelin your own customProductVariantPriceCalculationStrategy:import { PluginCommonModule, VendurePlugin } from '@vendure/core' import { CampaignPricePlugin, CampaignPriceStrategy } from '@haus-tech/campaign-price-plugin' import { PriceListProductVariantPriceCalculationStrategy } from './plugins/price-list/strategy/price-list-product-variant-price-calculation.strategy' @VendurePlugin({ imports: [PluginCommonModule], configuration: (config) => { config.catalogOptions.productVariantPriceCalculationStrategy = new PriceListProductVariantPriceCalculationStrategy( new CampaignPriceStrategy(config.catalogOptions.productVariantPriceCalculationStrategy), ) return config }, }) export class PricingCompositionPlugin {}Restart your Vendure server.
Usage
Price strategy composition
CampaignPricePlugin no longer overrides config.catalogOptions.productVariantPriceCalculationStrategy automatically.
This is intentional so projects can control precedence between campaign pricing and other pricing strategies.
You can use the helper directly in your own custom strategy:
import { getCampaignPriceForChannel } from '@haus-tech/campaign-price-plugin'
const campaignPrice = getCampaignPriceForChannel(args.productVariant, args.ctx)Admin UI
In the Admin UI, each product variant price has a Campaign Price field (per channel/currency). Set it to run a campaign; leave it empty or zero to use the ordinary price. The field uses the currency form input component.
Shop API
The plugin extends the Shop API so you can read both the current price (which may be the campaign price) and the ordinary price.
ProductVariant.ordinaryPrice—Int!— The ordinary (pre-campaign) price for the variant.Product.ordinaryPrice—SearchResultPrice!— Min/max ordinary price across the product’s variants (single value or range).SearchResult.ordinaryPrice—SearchResultPrice!— Ordinary price for search results. Requires the default search plugin to indexproduct-ordinaryPriceMinandproduct-ordinaryPriceMaxif you use search; add these to your search index configuration if needed.
Example: show “was / now” on a variant:
query VariantPricing($id: ID!) {
productVariant(id: $id) {
id
price
priceWithTax
ordinaryPrice
}
}Example: product with variant price range:
query ProductPricing($id: ID!) {
product(id: $id) {
id
priceRange {
min
max
}
ordinaryPrice {
... on SinglePrice {
value
}
... on PriceRange {
min
max
}
}
}
}Use price / priceRange for the current selling price (campaign or ordinary) and ordinaryPrice for the original price in your “was X, now Y” UI.
Testing
- Run
yarn testto execute the tests. - Implement additional tests to cover your specific use cases.
Publish to NPM
Make sure you are logged in to NPM.
Build the plugin:
yarn buildPublish the plugin:
yarn publish
Resources
- Vendure Plugin Documentation
- GraphQL Code Generator for generating TypeScript types for custom GraphQL types.
- Vendure Price Calculation for custom strategies
