2013-05-24 14 views
6

Tiếp theo các tài liệu trên trang này ... http://guides.spreecommerce.com/developer/calculators.htmlĐăng ký một tính tùy chỉnh Spree không làm việc

tôi đã tạo ra một lớp trong mô hình/Spree/máy tính/

module Spree 
class Calculator::ZipTax < Calculator 
    def self.description 
    "Calculates Tax Rates From Zipcode in TaxRates Table" 
    end 
    def compute(computable) 
    case computable 
    when Spree::Order 
     compute_order(computable) 
    when Spree::LineItem 
     compute_line_item(computable) 
    end 
end 
    def compute_order(order) 
    zipcode = order.bill_address.zipcode[0,5] 
    zip = TaxTable.where(:zipcode => zipcode).first 
    if(zip.present?) 
     rate = zip.combined_rate 
     order.line_items.sum(&:total) * rate 
    else 
     0 
    end 

end 
    end 
end 

Và trong initializers/Spree .rb Tôi đã thêm:

config = Rails.application.config 
config.spree.calculators.tax_rates << Spree::Calculator::ZipTax 

Nhưng tôi không thể bắt đầu Rails. Tôi nhận được phương thức undefined `< < 'cho nil: NilClass (NoMethodError) trên tập tin initializer/spree.rb.

Làm cách nào để đăng ký Máy tính tùy chỉnh? Sử dụng Spree 1.3.2.

Trả lời

9

Bạn sẽ cần phải quấn cấu hình của bạn trong một after_initialize:

trong config/application.rb

config.after_initialize do 
    config.spree.calculators.tax_rates << Spree::Calculator::ZipTax 
end 

Bạn đang gặp thông báo lỗi vì tính Spree chưa được khởi tạo ở đó chỉ trong quá trình khởi động ứng dụng của bạn, vì vậy bạn đang cố gắng nối thêm máy tính vào thứ gì đó không phải.

Một phương pháp, thường được sử dụng trong phần mở rộng Spree là phải làm như sau:

initializer 'spree.register.calculators' do |app| 
    app.config.spree.calculators.shipping_methods << Spree::Calculator::ZipTax 
end 
+0

Cảm ơn rất nhiều. Lần sau tôi sẽ biết rõ hơn là theo dõi các tài liệu của Spree một cách chặt chẽ;) –

+2

Trên một lưu ý phụ cho máy tính giao hàng, hãy đảm bảo rằng tên lớp là 'Spree :: Shipping :: ' cho máy tính của bạn để hiển thị trong bảng điều khiển quản trị https://github.com/spree/spree/blob/2a3f91229f65e1c0c7e16eb47da455d3488195bc/core/app/models/spree/shipping_method.rb#L50. Chẳng bao lâu để phát hành các phiên bản Spree chỉ yêu cầu máy tính giao hàng kế thừa từ 'Spree :: ShippingCalculator' https://github.com/spree/spree/blob/82fd0e0f762f17493c1609a998192272dab83e07/core/app/models/spree/shipping_method.rb#L50 – davidtingsu

+2

I nghĩ rằng điều này thực sự nên là một tên lớp của Spree :: Calculator :: Shipping :: để làm cho nó hiển thị trong Admin Panel. – lightcap