Discount

There are a number of methods for specifying discount.  For starters, there are 3 variables you can set for calculating discount: discountFlatRatediscountQuantityRate, and discountTotalRate.  You can also set a discountCustom function.  These 4 methods will be added up to create the total discount cost

To set a basic flat rate discount, you can use discountFlatRate.

``````evoShop({
discountFlatRate: 10
});``````

Remember, you can set this variable at any time. (It may be useful to change it when a user sets their discount state or country)

For a discount rate based on the total quantity of the cart (i.e. discount quantity rate of \$3 = \$21 for 7 items), use discountQuantityRate:

``````evoShop({
discountQuantityRate: 3
});``````

If you want a discount rate that is a percentage of the total cost of the cart, use discountTotalRate:

``````evoShop({
discountTotalRate: 0.1
});``````

You can also add a custom function that returns a discount cost based on arbitrary criteria. Let’s say you wanted to have the cost of discount be \$10, unless there are more than 20 items in the cart:

``````evoShop({
discountCustom: function(){
if( evoShop.quantity() > 20 ){
return 0;
} else {
return 10;
}
}
});
``````

You can also set this custom discount function using a shortcut in the .discount() function:

``````evoShop.discount(function(){
if( evoShop.quantity() > 20 ){
return 0;
} else {
return 10;
}
});
``````

If you want to calculate discount based on the individual items, you can modify the evoShop.Item object to have items calculate their own discount. Let’s say you wanted to have the discount for each item be 2 if the item’s size is “small”, and 5 if the size is anything else:

``````evoShop.Item._.discount = function(){
if( this.get( 'size' ) == "small" ){
return this.quantity()*2;
} else {
return this.quantity()*5;
}
};
``````

Of course, you could achieve the same effect by creating a custom discount method for evoShop that iterates through all the items:

``````evoShop.discount(function(){
var total = 0;
evoShop.each( function( item ){
if( item.get('size') == 'small' )[
total+= item.quantity()*2;
} else {
total+= item.quantity()*5;
}
});
``````evoShop.discount(); // returns current discount cost