Sabo
09/23/2022, 1:41 PMShippingCalculator
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
Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
I get :
uninitialized constant Shop::Calculator (NameError)
Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
kennyadsl
Sabo
09/23/2022, 2:07 PMmodule 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
kennyadsl
module Shop
module Calculator
module Shipping
class CustomCalculator < ...
Sabo
09/23/2022, 4:14 PMkennyadsl
Rails.application.config.to_prepare do
Rails.application.config.spree.calculators.shipping_methods << Shop::Calculator::Shipping::CustomCalculator
end
kennyadsl
kennyadsl
Sabo
09/23/2022, 6:59 PMSabo
09/23/2022, 7:02 PMkennyadsl
Sabo
09/24/2022, 7:29 AMwaiting_for_dev
09/27/2022, 4:34 AMwaiting_for_dev
09/27/2022, 4:39 AMString
instead of the class itself. So, as an alternative, you can also have:
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:
Rails.application.config.to_prepare do
Rails.application.config.spree.calculators.shipping_methods |= [Shop::Calculator::Shipping::CustomCalculator]
end
waiting_for_dev
09/27/2022, 4:39 AM