Spree::Calculatormodel and there are several subclasses provided to deal with various types of calculations (flat rate, percentage discount, sales tax, VAT, etc.) All calculators extend the
Spree::Calculatorclass and must provide the following methods:
calculableobject, which are typically one of
Spree::Promotion::Actions::CreateAdjustment. These three classes use the
Spree::Core::CalculatedAdjustmentmodule described below to provide an easy way to calculate adjustments for their objects.
flat_percentand can be set like this:
currency. These can be set like this:
first_item: The discounted price of the first item(s).
additional_item: The discounted price of subsequent items.
max_items: The maximum number of items this discount applies to.
first_itempreference is set to $10, your
additional_itemspreference is set to $5, and your
max_itemspreference is set to 4, the total discount would be$25:
amount: The amount per item to calculate.
currency: The currency for this calculator.
calculableresponding to a
promotionmethod, which should return a
Spree::Promotion(or similar) object. This object should then return a list of rules, which should respond to a
productsmethod. This is used to return a result of matching products.
amountof 5 and there's an order with the following line items:
minimal_amount: The minimum amount for the line items total to trigger the calculator.
discount_amount: The amount to discount from the order if the line items total is equal to or greater than the
normal_amount: The amount to discount from the order if the line items total is less than the
currency: The currency for this calculator. Defaults to the currency you have set for your application with
minimal_amountpreference of $50, a
normal_amountpreference of $2, and a
discount_amountof$5. An order with a line items total of $60 would result in a discount of$5 for the whole order. An order of $20 would result in a discount of$2.
Spree::Calculatorclass and define
computemethods on that class:
Spree::ShippingCalculatorinstead, and define a
config/initializers/spree.rbinside your application (
configvariable defined for brevity):
app/models/spree/calculator/shipping/my_own_calculator.rbyou should call:
available?method inside your calculator:
Spree::Core::CalculatedAdjustmentsmodule into a model of your choosing.
has_oneassociation to a
calculatorobject, as well as some convenience helpers for creating and updating adjustments for objects. Assuming that an object has a calculator associated with it first, creating an adjustment is simple:
calculableamount is whatever object your