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:
$31
and the calculator was configured to have a flat percent amount of 10
, the discount would be $3.10
, because $31 x 10% = $3.10
.
amount
and currency
. These can be set like this:
first_item
: The discounted price of the first items.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
:
$10
for the first item$5
for each of the 3
subsequent items: $5 \* 3 = $15
$0
for the remaining 6
itemsamount
: 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.
The list of matching products is compared against the line items for the order being calculated. If any of the matching products are included in the order, they are eligible for this calculator. The calculation is this:
[matching product quantity] x [amount]
Every matching product within an order will add to the calculator’s total. For example, assuming the calculator has an amount
of 5 and there’s an order with the following line items:
$15.00
x 2
within matching products$10.00
x 1
within matching products$20.00
x 4
excluded from matching productsminimal_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 store currencyminimal_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.