How to add a custom calculator for shipping or taxes to Spree
# app/models/spree/calculators/custom_flexi_rate.rb
module Spree
class Calculator::CustomFlexiRate < Calculator
def self.description
"Custom FlexiRate"
end
def compute(line_items)
13.00
end
end
end
# config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Peelstar
class Application < Rails::Application
...
# Add a new calculator for custom shipping or tax options
initializer 'spree.register.calculators' do |app|
app.config.spree.calculators.shipping_methods << Spree::Calculator::CustomFlexiRate
# app.config.spree.calculators.tax_rates << Spree::Calculator::CustomTaxCalculator
end
end
end
config/application.rb
app/models/spree/calculators/[class_name].rb
Class must have the following methods:
def self.description
"Custom FlexiRate"
end
def compute(line_items)
13.00
end