@haus-tech/campaign-price-plugin
v1.0.2
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: 1.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. When a campaign price is set, it is used as the selling price instead of the ordinary price. The plugin also exposes 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. - Price calculation strategy — If a variant has a campaign price set and it is greater than zero, that price is used; otherwise the existing price calculation strategy is used.
- 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], }Restart your Vendure server.
Usage
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
