是否可以在 Spree 的子 ShippingCalculator 自定义 class 中添加新方法?

Is it possible to add new methods inside in a child ShippingCalculator custom class in Spree?

我创建了一个名为 CorreiosPAC 的新自定义 class,用于计算包裹成本和订单的交货时间(订单的截止日期)(查看下面的代码)。我在 ShippingRate 模型中添加了一个名为 delivery_time 的新字段来存储交货时间。但是,我无法保存此值以在视图中显示给客户端。这是我最初想到的解决方案。我需要一个最佳解决方案,一个模式解决方案狂欢,我不知道该怎么做。

models/calculator/shipping/correios_pac.rb

require 'correios-frete'

module Spree
  module Calculator::Shipping

    class CorreiosPAC < ShippingCalculator
      attr_reader :delivery_time

      def self.description
        # Human readable description of the calculator
        'Entrega Econômica (PAC)'
      end

      def compute_package(package)
        # Returns the value after performing the required calculation

        altura = package.quantity * 2 + 1
        peso = (200.0 * package.quantity) / 1000 + 0.2
        cep_origem = '65086-110'
        cep_destino = package.order.shipping_address.zipcode

        @frete = Correios::Frete::Calculador.new( :cep_origem => cep_origem,
          :cep_destino => cep_destino,
          :peso => peso,
          :comprimento => 30,
          :largura => 30,
          :altura => altura).calcular :pac

        @delivery_time = @frete.prazo_entrega
        @frete.valor
      end
    end

  end
end

models/shipping_rate_decorator.rb

Spree::ShippingRate.class_eval do
  attr_accessor :delivery_time 
end

您不应将送货时间存储在计算器中,因为它用于计算运费。

地址转换完成后可以直接调用存储远程api Correios::Frete::Calculador(需要迁移)

然后您可以从计算器中的包访问订单对象:

def compute_package(package)
...
package.order.frete.valor
...
end