From cc2e8694f06b35c3a36707a5c6eea481db1a0d63 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 20 May 2013 15:01:04 +1000 Subject: [PATCH] Move LineItem update to source of change (order update). On empty cart, re-instanciate shipping method adjustment, fixing no shipping fees after empty cart bug. --- app/models/spree/line_item_decorator.rb | 11 ++++------- app/models/spree/order_decorator.rb | 14 ++++++++++++++ spec/features/consumer/checkout_spec.rb | 23 ++++++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 7bda8c2a06..ec4f83e5e2 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -7,17 +7,14 @@ Spree::LineItem.class_eval do def itemwise_shipping_cost - # When order has not yet been placed, update shipping method in case order - # has changed to a distributor with a different shipping method - if %w(cart address delivery resumed).include? self.order.state - set_itemwise_shipping_method - save! if shipping_method_id_changed? - end - order = OpenStruct.new :line_items => [self] shipping_method.compute_amount(order) end + def update_itemwise_shipping_method_without_callbacks!(distributor) + update_column(:shipping_method_id, self.product.shipping_method_for_distributor(distributor).id) + end + private diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 5d26b9af27..e50b4350f4 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -7,9 +7,16 @@ Spree::Order.class_eval do attr_accessible :distributor_id before_validation :shipping_address_from_distributor + before_save :update_line_item_shipping_methods after_create :set_default_shipping_method + def empty! + line_items.destroy_all + adjustments.destroy_all + set_default_shipping_method + end + def products_available_from_new_distributor # Check that the line_items in the current order are available from a newly selected distributor errors.add(:distributor_id, "cannot supply the products in your cart") unless DistributorChangeValidator.new(self).can_change_to_distributor?(distributor) @@ -66,4 +73,11 @@ Spree::Order.class_eval do end end end + + def update_line_item_shipping_methods + if %w(cart address delivery resumed).include? state + self.line_items.each { |li| li.update_itemwise_shipping_method_without_callbacks!(distributor) } + self.update! + end + end end diff --git a/spec/features/consumer/checkout_spec.rb b/spec/features/consumer/checkout_spec.rb index ed44f18d6a..a016206da9 100644 --- a/spec/features/consumer/checkout_spec.rb +++ b/spec/features/consumer/checkout_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" feature %q{ As a consumer - I want select a distributor for collection + I want to select a distributor for collection So that I can pick up orders from the closest possible location } do include AuthenticationWorkflow @@ -116,6 +116,27 @@ feature %q{ page.should have_selector 'span.shipping-total', text: '$4.68' end + scenario "adding a product to cart after emptying cart shows correct delivery fees" do + # When I add a product to my cart + login_to_consumer_section + click_link @product_1.name + select @distributor.name, :from => 'distributor_id' + click_button 'Add To Cart' + + # Then I should see the correct delivery fee + page.should have_selector 'span.grand-total', text: '$20.99' + + # When I empty my cart and add the product again + click_button 'Empty Cart' + click_link 'Continue shopping' + click_link @product_1.name + select @distributor.name, :from => 'distributor_id' + click_button 'Add To Cart' + + # Then I should see the correct delivery fee + page.should have_selector 'span.grand-total', text: '$20.99' + end + scenario "buying a product", :js => true do login_to_consumer_section