2012-11-04 12 views
12

Tôi đang cố thêm một bước bổ sung vào cửa hàng Spree 1.2 của mình, điều này sẽ cho phép khách hàng tạo đăng ký. Tôi đã chèn bước và hiển thị chế độ xem chính xác nhưng khi người dùng nhấp vào 'lưu và tiếp tục', bước tiếp theo sẽ được hiển thị, nhưng không có gì thực sự được lưu.Thêm một bước vào quy trình thanh toán Spree

Tôi hiểu rằng tôi cần phải thêm một state_callback, nhưng tôi không chắc chắn làm thế nào để làm điều này và các tài liệu Spree là rất thiếu khoảng này (có lẽ vì nó khá mới)

Hiện nay tôi đã có sau trong phần mở rộng của tôi:

mô hình/Spree/order_decorator.rb

Spree::Order.class_eval do 
    belongs_to :subscription 

    accepts_nested_attributes_for :subscription 

    # This doesn't appear to be called 
    Spree::Order.state_machine.after_transition :from => :subscription, 
               :do => :valid_subs? 

    checkout_flow do 
    go_to_state :address 
    go_to_state :subscription 
    go_to_state :payment, :if => lambda { |order| order.payment_required? } 
    go_to_state :confirm, :if => lambda { |order| order.confirmation_required? } 
    go_to_state :complete 
    remove_transition :from => :delivery, :to => :confirm 
    end 
end 

Không hoàn toàn chắc chắn rằng accepts_nested_attributes là cần thiết, nhưng cách tiếp cận dev của tôi cho điều này đã được thử và sai cho đến nay, vì vậy nó đã kết thúc Ở đó.

Trong mô hình/subscription.rb

class Subscription < ActiveRecord::Base 

    attr_accessible :start_date, :frequency 

    belongs_to :user 
    has_many :orders 
    has_many :products 

    validates :start_date, :frequency, :presence => true 

    def schedule 
    ...code that returns a list of dates rendered on FE... 
    end 

    private #---- 

    ... some methods used in schedule ... 

    def valid_subs? 
    binding.pry # never called 
    end 

    def after_subscription 
    binding.pry # never called either... 
    end 
end 

views/Spree/kiểm tra/_subscription.html.erb

<h1><%= t(:"subscription.title") %></h1> 

<div class="columns alpha six" data-hook="subscription_calendar_fieldset_wrapper"> 
    <fieldset id="subscription_calendar" data-hook> 
    <%= form.fields_for :subscription_picker do |subscription_picker| %> 
     <legend><%= t(:"subscription.first_delivery") %></legend> 
     <%= render :partial => 'subscription/picker' %> 
    <% end %> 
    </fieldset> 
</div> 

<div class="columns omega six" data-hook="subscription_dates_fieldset_wrapper"> 
    <fieldset id="subscription_dates" data-hook> 
    <legend align="center"><%= t(:"subscription.next_deliveries") %></legend> 
    <div class='dates'></div> 
    </fieldset> 
</div> 

<div class="form-buttons" data-hook="buttons" style='clear:both;'> 
    <%= submit_tag t(:save_and_continue), :class => 'continue button primary' %> 
</div> 

views/thuê bao/_picker.html.erb

<div class='row'> 
    <label for="subscription_frequency">Occurs every:</label> 
    <% frequency_options = [["2 Weeks", 14], ["3 Weeks", 21], ["Month", 30], ["2 Months", 60], ["3 Months", 90]] %> 
    <%= select(:subscription, :frequency, options_for_select(frequency_options, 30), {}) %> 
</div> 
<div id="start-date-picker" class="calendar"></div> 
<%= hidden_field(:subscription, :start_date, {value: (DateTime.now + 14).to_date.iso8601}) %> 

... JS that creates the calendar ... 

Khi nhấp vào 'lưu và tiếp tục' tôi thấy các thông số sau được gửi:

{ 
        "utf8" => "✓", 
       "_method" => "put", 
    "authenticity_token" => "...BLAH...", 
      "subscription" => { 
     "frequency" => "30", 
     "start_date" => "2012-11-17" 
    }, 
       "commit" => "Save and Continue", 
      "controller" => "spree/checkout", 
       "action" => "update", 
       "state" => "subscription" 
} 
+0

Bạn có thử nghiệm tự động cho trang trí của mình không? Bạn thấy gì trong các bài kiểm tra? Các bản ghi có được lưu ở cấp độ thử nghiệm đơn vị không? – WattsInABox

Trả lời

3

Bạn có hai vấn đề. Gọi lại của bạn không kích hoạt vì lệnh gọi checkout_flow sẽ xóa máy trạng thái hiện tại và thay thế nó. Di chuyển mã này đến sau khi bạn gọi đến checkout_flow:

Spree::Order.state_machine.after_transition :from => :subscription, 
              :do => :valid_subs? 

Thứ hai, thông tin đăng ký của bạn không được lưu vì thông số cần được chuyển như một phần của đơn đặt hàng. Các params nên xuất hiện lồng như thế này:

"order" => {  
    "subscription" => { 
     "frequency" => "30", 
     "start_date" => "2012-11-17" 
    } 
} 

Bạn có thể làm điều đó bằng cách gắn cuộc gọi của bạn để chọn và hidden_field mẫu 'subscription_picker' thích hợp:

<%= subscription_picker.select(:frequency, options_for_select(frequency_options, 30), {}) %> 

<%= subscription_picker.hidden_field(:start_date, {value: (DateTime.now + 14).to_date.iso8601}) %> 

Có lẽ bạn nên truyền đối tượng biểu mẫu cho phần của bạn như một tham số rõ ràng cho kiểu dáng và khả năng hiểu.

Chúc mừng.