I created a custom `ShippingCalculator` following ...
# support
s
I created a custom
ShippingCalculator
following the instructions in https://guides.solidus.io/developers/shipments/custom-shipping-calculators The custom ShippingCalculator (seems) work correctly in the rails console
Shop::Calculator::Shipping::CustomCalculator
but when I try to register it in
intializers/spree.rb
by
Copy code
Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
I get :
Copy code
uninitialized constant Shop::Calculator (NameError)
Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
k
Can you please show how the calculator is defined and its location in the filesystem?
s
shop/app/models/shop/calculator/shipping/custom_calculator.rb
Copy code
module Shop
	class Calculator::Shipping::CustomCalculator < Spree::ShippingCalculator
		preference :currency, :string, default: -> { Spree::Config[:currency] }
	
		def self.description
			"Calculateur de livraison pour France (FR)"
		end
		
		def compute_package(package)
			package.weight / 50
		end	
		
	end
end
k
can you try writing the modules one per line?
Copy code
module Shop
  module Calculator
    module Shipping
      class CustomCalculator < ...
๐Ÿ‘ 1
s
I get the same result
k
Ok, can you please try:
Copy code
Rails.application.config.to_prepare do
  Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
end
๐Ÿ‘ 1
From Rails 7, this is required
and yes, guides should reflect that, can you please let me know if this solves?
๐Ÿ‘ 1
s
Ok I have Rails 7.0.4 and Solidus 3.2.2 I will try
It's working thank you @kennyadsl ๐Ÿ˜‰
k
Credits to @waiting_for_dev for suggesting me that solution
โค๏ธ 1
s
Thank you @waiting_for_dev x)
โค๏ธ 1
w
They propose an alternative configuration, pushing a
String
instead of the class itself. So, as an alternative, you can also have:
Copy code
Rails.application.config.spree.calculators.shipping_methods << 'Shop::Calculator::Shipping::CustomCalculator'
If you go with the
<http://config.to|config.to>_prepare
variant, be aware that the block is run at every code reload. That means the class will be pushed more than once in development. I think that wonโ€™t change your app behavior, but a to be safer:
Copy code
Rails.application.config.to_prepare do
  Rails.application.config.spree.calculators.shipping_methods |= [Shop::Calculator::Shipping::CustomCalculator]
end
๐Ÿ‘ 1
The latter will only push the `CustomCalculator` class unless itโ€™s already there.