Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Last active August 31, 2017 09:53
Show Gist options
  • Save wrburgess/4600383 to your computer and use it in GitHub Desktop.
Save wrburgess/4600383 to your computer and use it in GitHub Desktop.
How to add a custom calculator for shipping or taxes to Spree
  1. Add initializer to config/application.rb
  2. Add new class to app/models/spree/calculators/[class_name].rb
  3. Add logic to class file

Class must have the following methods:

def self.description
  "Custom FlexiRate"
end

def compute(line_items)
  13.00
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
# 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
@lynndylanhurley
Copy link

Instead of this path:

app/models/spree/calculators/custom_flexi_rate.rb

It should be this path:

app/models/spree/calculator/custom_flexi_rate.rb

Notice calculators (plural) -> calculator (singular).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment