ai

Unchained is hackable

With hackable we mean to provide you with only very basic customization out of the box but on the other hand allow the developer to completely customize the business logic through code. This is achieved through a staged approach ranging from plugins to a full hard fork.

See how easy it is to implement an extremely custom pricing module with Unchained Engine below.

Weather-dependent barbeque sausage pricing

Usually when summer kicks in in Switzerland and people start to invite friends to the first barbeque party of the year, demand for sausages surges instantly. Our dear friend, butcher Bruno wants to sell his homemade sausages directly to the customer with his online shop and had this crazy idea of a special product pricing he calls weather-dependent barbeque sausage pricing.

Add a pricing module

To demonstrate, we will implement an ingenious pricing that increases the catalog price by 1 USD if the temperature reaches 20 degrees celsius in Zürich:

import {
  ProductPricingDirector,
  ProductPricingAdapter,
} from 'meteor/unchained:core-pricing';
import fetch from 'isomorphic-unfetch';

const PRODUCT_TAG_SAUSAGE = 'sausage';
const SAUSAGE_THRESHOLD_CELSIUS = 20;

class WeatherDependentBarbequeSausagePricing extends ProductPricingAdapter {
  static key = 'shop.unchained.wd-bbq-sausage-pricing'
  static version = '1.0'
  static label = 'Calculate the price of a sausage 🌭🌦'
  static orderIndex = 3

  static isActivatedFor(product) {
    if (
      product.tags &&
      product.tags.length > 0 &&
      product.tags.indexOf(PRODUCT_TAG_SAUSAGE) !== -1) {
      return true;
    }
    return false;
  }

  async calculate() {
    const { currency, quantity } = this.context;
    try {
      const response = await fetch('https://query.yahooapis.com/v1/public/yql?q=select%20item.condition.temp%20from%20weather.forecast%20where%20woeid%20%3D%20784794&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
      if (response.status === 200) {
        const { query } = await response.json();
        const { results, count } = query;
        if (count > 0) {
          const { temp: tempFahrenheit } = results.channel.item.condition;
          const tempCelsius = (parseFloat(tempFahrenheit) - 32) / 1.8;
          if (tempCelsius > SAUSAGE_THRESHOLD_CELSIUS) {
            console.log('🌭 -> High season, sausage pricy!!'); // eslint-disable-line
            this.result.addItem({
              currency,
              amount: +100 * quantity,
              isTaxable: true,
              isNetPrice: true,
              meta: { adapter: this.constructor.key },
            });
          }
        }
      }
    } catch (e) {
      console.error('🌭 -> Failed while trying to price weather dependent'); // eslint-disable-line
    }

    return super.calculate();
  }
}

ProductPricingDirector.registerAdapter(WeatherDependentBarbequeSausagePricing);

Craft the query

Unchained Commerce is headless by design. So in order to get the product data from our Unchained Engine, we need to craft a small GraphQL query that actually resolves some texts and the current price of our sausage.

query getProduct {
  product(slug: "🌭") {
    _id
    texts {
      title
    }
    ... on SimpleProduct {
      catalogPrice {
        price {
          amount
          currency
        }
      }
    }
  }
}
Try it out live @ GraphQL Bin

Build the UI

Because Unchained Commerce is headless, you can build any UI on any operating system that can somehow connect to the HTTP endpoint. How awesome is that? Let's just use a small widget to display some product details on our page right here:

BBQ Sausage (digital)

The best sausage in town

CHF5.00

Just to be sure to tell you, we build digital commerce and don't sell sausages, so if you buy this you will not get a sausage that you can eat, instead we will send you a customized svg file of a sausage together with a license.

Checkout our very own ProductDetails component used throughout unchained.shop

We are working on the Documentation. Stay tuned.

Want to get reminded when we have news to share? Follow our twitter account @unchained_shop.

Want to help? Get over to our Issues List and let's see what we can do together.

Open Source

available on Github