Created
March 4, 2012 22:51
-
-
Save dhonig/1975227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spree::Order.class_eval do | |
# order state machine (see http://github.com/pluginaweek/state_machine/tree/master for details) | |
Spree::Order.state_machines[:state] = StateMachine::Machine.new(Spree::Order, :initial => 'cart') do | |
event :next do | |
transition :from => 'cart', :to => 'checkout_method' | |
transition :from => 'checkout_method', :to => 'shipping_address' | |
transition :from => 'shipping_address', :to => 'delivery' | |
transition :from => 'delivery', :to => 'payment', :if => :payment_required? | |
transition :from => 'payment', :to => 'confirm' | |
# note: some payment methods will not support a confirm step | |
transition :from => 'payment', :to => 'confirm', | |
:if => Proc.new { |order| order.payment_method && order.payment_method.payment_profiles_supported? } | |
transition :from => 'confirm', :to => 'complete' | |
end | |
event :cancel do | |
transition :to => 'canceled', :if => :allow_cancel? | |
end | |
event :return do | |
transition :to => 'returned', :from => 'awaiting_return' | |
end | |
event :resume do | |
transition :to => 'resumed', :from => 'canceled', :if => :allow_resume? | |
end | |
event :authorize_return do | |
transition :to => 'awaiting_return' | |
end | |
before_transition :to => 'complete' do |order| | |
begin | |
order.process_payments! | |
rescue Core::GatewayError | |
if Spree::Config[:allow_checkout_on_gateway_error] | |
true | |
else | |
false | |
end | |
end | |
end | |
before_transition :to => ['delivery'] do |order| | |
order.shipments.each { |s| s.destroy unless s.shipping_method.available_to_order?(order) } | |
end | |
after_transition :to => 'complete', :do => :finalize! | |
after_transition :to => 'delivery', :do => :create_tax_charge! | |
after_transition :to => 'payment', :do => :create_shipment! | |
after_transition :to => 'resumed', :do => :after_resume | |
after_transition :to => 'canceled', :do => :after_cancel | |
end | |
before_validation :clone_shipping_address, :if => :use_shipping? | |
attr_accessor :use_shipping | |
def formated_subtotal | |
"$0.00" | |
end | |
def formated_promo_amount | |
"$0.00" | |
end | |
def formated_total | |
"$0.00" | |
end | |
def clone_shipping_address | |
if ship_address and self.ship_address.nil? | |
self.bill_address = ship_address.clone | |
else | |
self.bill_address.attributes = ship_address.attributes.except('id', 'updated_at', 'created_at') | |
end | |
true | |
end | |
def use_shipping? | |
@use_shipping == true || @use_shipping == "true" || @use_shipping == "1" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment