npm package: @haus-tech/campaign-price-plugin
Description: Campaign price plugin
Author: Haus Tech
Latest version: 2.0.0

---
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.2
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) to `ProductVariantPrice`, 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`, and `SearchResult` get an `ordinaryPrice` field 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:

1. Install the plugin package:

   ```bash
   yarn add @haus-tech/campaign-price-plugin
   ```

   Or, if using npm:

   ```bash
   npm install @haus-tech/campaign-price-plugin
   ```

2. Add the plugin to your Vendure configuration in `vendure-config.ts`:

   ```ts
   import { CampaignPricePlugin } from '@haus-tech/campaign-price-plugin'

   export const config = {
     plugins: [CampaignPricePlugin],
   }
   ```

3. 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 index `product-ordinaryPriceMin` and `product-ordinaryPriceMax` if you use search; add these to your search index configuration if needed.

Example: show “was / now” on a variant:

```graphql
query VariantPricing($id: ID!) {
  productVariant(id: $id) {
    id
    price
    priceWithTax
    ordinaryPrice
  }
}
```

Example: product with variant price range:

```graphql
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

1. Run `yarn test` to execute the tests.
2. Implement additional tests to cover your specific use cases.

## Publish to NPM

1. Make sure you are [logged in to NPM](https://docs.npmjs.com/cli/v9/commands/npm-login).
2. Build the plugin:

   ```bash
   yarn build
   ```

3. Publish the plugin:

   ```bash
   yarn publish
   ```

## Resources

- [Vendure Plugin Documentation](https://www.vendure.io/docs/plugins/)
- [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) for generating TypeScript types for custom GraphQL types.
- [Vendure Price Calculation](https://www.vendure.io/docs/products-stock/pricing/) for custom strategies