Pricing Rules - Wuntenn/acme-shopping-basket GitHub Wiki
Pricing rules
The pricing rules are structured as an object-literal. Doing adds an expressive nature to the rule content. The attributes in this object are the products
and promotions
keys.
{
products: [],
promotions: []
}
Products
The products
attribute references an array of product objects. They are to be structured as follows:
{
productCode: 'FR1',
name: 'Fruit tea',
price: 3.11,
}
productCode
: Is a string used to represent the product. This is commonly known as a SKU (stock keeping unit) code.name
: This references a string version of the product nameprice
: This references a number that represents the price of the product.
The example above represents the Fruit Tea product. It has been stored with the product code FR1 and has a price of £3.11.
Errors
Need an array of products (Format: productCode, name, price)
This error occurs where you don't have the key products
in your pricing rules, or happens where your pricing rules are not an array. To fix it please structure your products as above.
Promotions
The promotions
attribute references an array of functions or references to promotion functions.
A promotion function is a function for calculating the reductions related to a product or group of products. The function uses the basket contents and product detail as it's inputs and should return an amount of deduction based on the basket content and promotion.
function examplePromofunction(basketItems, itemsDb) {
// do some calculation based on the basket contents and product informtation
return discount;
}
Each promotion function is applied consecutively and the deductions returned by them are summed to arrive at the final discount.
So we end up with pricing rules that looks like:
var pricingRules = {
products: [{ product 1 }, { product 2 }, { product N } ],
promotions: [function promo1() { }, function promo2() {}, function promoN() { }]
}
If you opt to use references instead, we end up with:
// somewhere in the codebase
function promo1() { }
function promo2() {}
function promoN() { }
var pricingRules = {
products: [{ product 1 }, { product 2 }, { product N } ],
promotions: [ promo1, promo2, promoN ]
}