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.

This commit is contained in:
Rohan Mitchell
2013-05-20 15:01:04 +10:00
parent 571dfd9e9f
commit cc2e8694f0
3 changed files with 40 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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