Mauro
Mauro

Reputation: 171

Custom Calculator with Spree

I have some problem to create a custom calculator. I follow the instructions here:

http://guides.spreecommerce.org/developer/calculators.html.

I have my calculator inside spree/calculator:

class CustomCalculator < Spree::Calculator
  def self.description
    # Human readable description of the calculator
  end

  def compute(object=nil)
    p "Test"
    10.0
    # Returns the value after performing the required calculation
  end
end

and in spree.rb I added:

config = Rails.application.config
config.spree.calculators.tax_rates << CustomCalculator

but when I run the server, I receive this server:

config/initializers/spree.rb:24:in `<top (required)>': uninitialized constant CustomCalculator (NameError)

I already looked around and tries different ways to create my custom calculator... but nothing was right.

I'm using Spree 3.1.

Upvotes: 1

Views: 998

Answers (2)

Nimish Gupta
Nimish Gupta

Reputation: 3175

  1. Create a file with class_name in models/spree/calculators folder
    module Spree
      class Calculator::CustomCalculator < Calculator
        def self.description
          # Human readable description of the calculator
        end

        def compute(object=nil)
          p "Test"
          10.0
          # Returns the value after performing the required calculation
        end
      end
    end
  1. In spree.rb add Rails.application.config.spree.calculators.tax_rates << Spree::Calculator::CustomCalculator

Upvotes: 0

Jatin
Jatin

Reputation: 56

Since you have your calculator inside spree/calculator, do scope it under Spree::Calculator:

module Spree
  class Calculator::CustomCalculator < Calculator
    def self.description
      # Human readable description of the calculator
    end

    def compute(object=nil)
      p "Test"
      10.0
      # Returns the value after performing the required calculation
    end
  end
end

and in spree.rb add:

Spree::Core::Engine.config.after_initialize do
  config = Rails.application.config
  config.spree.calculators.tax_rates << Spree::Calculator::CustomCalculator
end

Upvotes: 2

Related Questions