Spree::Calculator
model 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::Calculator
class and must provide the following methods:calculable
object, which are typically one of Spree::ShippingMethod
, Spree::TaxRate
, or Spree::Promotion::Actions::CreateAdjustment
. These three classes use the Spree::Core::CalculatedAdjustment
module described below to provide an easy way to calculate adjustments for their objects.flat_percent
and can be set like this:amount
and 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.max_items
first_item
preference is set to $10, your additional_items
preference is set to $5, and your max_items
preference is set to 4, the total discount would be$25:amount
: The amount per item to calculate.currency
: The currency for this calculator.calculable
responding to a promotion
method, which should return a Spree::Promotion
(or similar) object. This object should then return a list of rules, which should respond to a products
method. This is used to return a result of matching products.amount
of 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 minimal_amount
.normal_amount
: The amount to discount from the order if the line items total is less than the minimal_amount
.currency
: The currency for this calculator. Defaults to the currency you have set for your application with Spree::Config[:currency]
minimal_amount
preference of $50, a normal_amount
preference of $2, and a discount_amount
of$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::Calculator
class and define description
and compute
methods on that class:Spree::ShippingCalculator
instead, and define a compute_package
method:config/initializers/spree.rb
inside your application (config
variable defined for brevity):app/models/spree/calculator/shipping/my_own_calculator.rb
you should call:available?
method inside your calculator:Spree::Core::CalculatedAdjustments
module into a model of your choosing.has_one
association to a calculator
object, 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:create_adjustment
, update_adjustment
and compute_amount
will call compute
on the Calculator
object. This calculable
amount is whatever object your CustomCalculator
class supports.