Custom Pricing Module · Example

Weather‑Dependent Pricing

See how easy it is to build custom business logic with Unchained's extensible architecture. This example shows a weather‑dependent pricing module for seasonal products.

Add a Pricing Module

Create a custom pricing adapter that adjusts prices based on weather conditions. This demonstrates how to extend Unchained's pricing system with external APIs.

import {
  ProductPricingAdapter,
  ProductPricingDirector,
} from '@unchainedshop/core';

const PRODUCT_TAG_SAUSAGE = 'sausage';
const SAUSAGE_THRESHOLD_CELSIUS = 20;
// Current temperature in Zurich, no API key needed
const WEATHER_URL =
  'https://api.open-meteo.com/v1/forecast' +
  '?latitude=47.37&longitude=8.54&current=temperature_2m';

const currentTemperature = async () => {
  const response = await fetch(WEATHER_URL);
  const { current } = await response.json();
  return current.temperature_2m;
};

const WeatherDependentSausagePricing = {
  ...ProductPricingAdapter,

  key: 'shop.unchained.wd-bbq-sausage-pricing',
  version: '1.0.0',
  label: 'Calculate the price of a sausage 🌭🌦',
  orderIndex: 3,

  isActivatedFor: ({ product }) =>
    Boolean(product.tags?.includes(PRODUCT_TAG_SAUSAGE)),

  actions: (params) => {
    const pricingAdapter = ProductPricingAdapter.actions(params);

    return {
      ...pricingAdapter,

      calculate: async () => {
        const { quantity } = params.context;
        try {
          if ((await currentTemperature()) > SAUSAGE_THRESHOLD_CELSIUS) {
            // 🌭 High season: +1 CHF per sausage
            pricingAdapter.resultSheet().addItem({
              amount: 100 * quantity,
              isTaxable: true,
              isNetPrice: true,
              meta: { adapter: WeatherDependentSausagePricing.key },
            });
          }
        } catch (e) {
          console.error('🌭 Weather lookup failed, regular price', e);
        }

        return pricingAdapter.calculate();
      },
    };
  },
};

ProductPricingDirector.registerAdapter(WeatherDependentSausagePricing);

Craft the Query

Use GraphQL to fetch product information with the calculated prices. The pricing module automatically applies weather‑based adjustments to the catalog price.

query getProduct {
  product(slug: "🌭") {
    _id
    texts {
      title
    }
    ... on SimpleProduct {
      catalogPrice {
        amount
        currencyCode
      }
    }
  }
}

Build the UI

Create a frontend that displays the dynamically calculated prices. Use any framework you prefer (React, Vue, Angular or vanilla JavaScript), or start with Unchained Create.

Unchained Create on GitHub