Skip to content

Instantly share code, notes, and snippets.

@cwardzala
Last active June 2, 2016 19:13
Show Gist options
  • Save cwardzala/7a1411ece641b62307e5b9e00ff72908 to your computer and use it in GitHub Desktop.
Save cwardzala/7a1411ece641b62307e5b9e00ff72908 to your computer and use it in GitHub Desktop.
let code_types = {};
code_types.cart = (promo, products) => {
let products = productUtils.getProductsByType(products, promo.product_types);
let total = 0;
products.forEach((item) => {
total = total + productUtils.getTotal(item, promo.use_contract_price ? account.org.price_contracts[item.format] : 0);
});
if (
total < promo.min_total // total is less than the min_total
) {
// Promo cannot be applied.
return false;
}
return true;
};
code_types.order = (promo, products) => {
let total = promo.use_contract_price ? cartUtils.getGrandTotal(products) : cartUtils.getSubTotal(products);
if (
total < promo.min_total // total is less than the min_total
) {
// Promo cannot be applied.
return false;
}
return true;
};
let testPromoCode = (code, products) => {
let promo = promoUtils.getPromoByCode(code);
let future = Date.now() < new Date(promo.start_date).getTime();
let expired = Date.now() > new Date(promo.end_date).getTime();
if (
!products.length || // no products matching the product_types allowed
!promo.active || // active not true
future || // start_date in the future
expired || // end_date in the past
) {
return false;
}
return code_types[promo.type](this, promo, cart);
};
[{
"code": "KICKSTARTPW",
"type": "cart",
"active": true,
"start_date": "2015-10-29T17:59:02+0000",
"end_date": "null",
"min_total": 3500,
"product_types": [
"playaway",
"recorded_books"
],
"discount_ammount": 0.3,
"discount_type": "percent",
"use_contract_price": false,
"_id": "1234lkj12l34kjljk1l234"
},
{
"code": "WOOT10Off",
"type": "order",
"active": true,
"start_date": "2015-10-29T17:59:02+0000",
"end_date": null,
"min_total": 3500,
"product_types": null,
"discount_ammount": 0.1,
"discount_type": "percent",
"use_contract_price": false,
"_id": "1234lkj12l34kjljk1l234"
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment