![](/img/trans.png)
[英]How to Remove the first step of registration from the flow of checkout process in Spree?
[英]Adding a step to the Spree checkout process
我正在嘗試為我的Spree 1.2商店添加一個額外的步驟,這將允許客戶創建訂閱。 我已插入步驟,並呈現正確的視圖,但當用戶單擊“保存並繼續”時,將呈現下一步,但實際上並未保存任何內容。
我知道我需要添加一個state_callback,但我不知道如何做到這一點並且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
不完全確定accepts_nested_attributes是必要的,但到目前為止我的開發方法一直是試驗和錯誤,所以它最終停留在那里。
在models / 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
視圖/禮包/結帳/ _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>
意見/訂閱/ _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 ...
點擊“保存並繼續”后,我看到發送了以下參數:
{
"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"
}
你有兩個問題。 您的回調未觸發,因為對checkout_flow的調用會擦除當前狀態機並替換它。 在調用checkout_flow后移動此代碼:
Spree::Order.state_machine.after_transition :from => :subscription,
:do => :valid_subs?
其次,您的訂閱信息未保存,因為參數需要作為訂單的一部分傳遞。 params應該看起來像這樣嵌套:
"order" => {
"subscription" => {
"frequency" => "30",
"start_date" => "2012-11-17"
}
}
您可以通過將對select和hidden_field的調用附加到相應的表單'subscription_picker'來實現:
<%= subscription_picker.select(:frequency, options_for_select(frequency_options, 30), {}) %>
和
<%= subscription_picker.hidden_field(:start_date, {value: (DateTime.now + 14).to_date.iso8601}) %>
您應該將表單對象傳遞給partial,作為樣式和可理解性的顯式參數。
干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.