Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Last active August 31, 2017 09:53
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
@krtschmr
Copy link

krtschmr commented Jan 9, 2016

not working for me. spree 3-0-stable

@niquepa
Copy link

niquepa commented Feb 16, 2016

How do i know that the calculator is registered and is called?

I create a new calculator:

module Spree class Calculator::SizeRate < Spree::Calculator def self.description # Human readable description of the calculator "Calculadora segun el tamaño del banner" end def compute(object=nil) 99 end end end

and registered here:

config.spree.calculators.promotion_actions_create_adjustments << Spree::Calculator::SizeRate

but when i create an order or update something nothing happens

@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