diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 79c680b67a..4361daef82 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -1,6 +1,9 @@ Spree::Order.class_eval do belongs_to :distributor + after_create :set_default_shipping_method + + def can_change_distributor? # Distributor may not be changed once an item has been added to the cart/order line_items.empty? @@ -31,6 +34,20 @@ Spree::Order.class_eval do before_validation :shipping_address_from_distributor private + + # On creation of the order (when the first item is added to the user's cart), set the + # shipping method to the first one available and create a shipment. + # order.create_shipment! creates an adjustment for the shipping cost on the order, + # which means that the customer can see their shipping cost at every step of the + # checkout process, not just after the delivery step. + # This is based on the assumption that there's only one shipping method visible to the user, + # which is a method using the itemwise shipping calculator. + def set_default_shipping_method + self.shipping_method = Spree::ShippingMethod.where("display_on != 'back_end'").first + self.save! + self.create_shipment! + end + def shipping_address_from_distributor if distributor self.ship_address = distributor.pickup_address.clone diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 18b64aa03f..3ac47fa7c8 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,6 +1,19 @@ require 'spec_helper' describe Spree::Order do + it "initialises a default shipping method after creation" do + shipping_method_back_end = create(:shipping_method, :display_on => :back_end) + shipping_method_both = create(:shipping_method, :display_on => :both) + + subject.shipping_method.should be_nil + subject.adjustments.should be_empty + + subject.save! + + subject.shipping_method.should == shipping_method_both + subject.adjustments.where(:label => "Shipping").should be_present + end + it "reveals permission for changing distributor" do p = build(:product)