From d1e65691cfe4c06349667984a4e6358df01be024 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 8 Jun 2018 12:57:17 +0200 Subject: [PATCH] Copy distributor addr. when using a pick up method This copies the distributor's address when at least one of the order shipments uses a shipping method that requires shipping address. --- app/models/order_updater.rb | 8 +++++--- spec/models/order_updater_spec.rb | 27 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/models/order_updater.rb b/app/models/order_updater.rb index f27103385e..5534e8d2a4 100644 --- a/app/models/order_updater.rb +++ b/app/models/order_updater.rb @@ -44,9 +44,11 @@ class OrderUpdater < SimpleDelegator end def shipping_address_from_distributor - shipping_method = order.shipments.first.shipping_methods.first - return if shipping_method.require_ship_address + shipments.each do |shipment| + shipping_method = shipment.shipping_method + next if shipping_method.require_ship_address - order.ship_address = order.distributor.address + order.ship_address = order.distributor.address + end end end diff --git a/spec/models/order_updater_spec.rb b/spec/models/order_updater_spec.rb index bb1e01ea5c..054154aadd 100644 --- a/spec/models/order_updater_spec.rb +++ b/spec/models/order_updater_spec.rb @@ -101,24 +101,43 @@ describe OrderUpdater do context '#before_save_hook' do let(:distributor) { build(:distributor_enterprise) } - let(:shipment) { build(:shipment) } let(:order) { build(:order, distributor: distributor) } + let(:shipment) { build(:shipment) } + let(:shipping_rate) do + Spree::ShippingRate.new( + shipping_method: shipping_method, + selected: true + ) + end before do - shipment.shipping_methods << shipping_method + shipment.shipping_rates << shipping_rate order.shipments << shipment end - context 'when the shipping method doesn\'t require a delivery address' do + context 'when any of the shipping methods doesn\'t require a delivery address' do let(:shipping_method) { build(:base_shipping_method, require_ship_address: false) } + let(:delivery_shipment) { build(:shipment) } + let(:delivery_shipping_rate) do + Spree::ShippingRate.new( + shipping_method: build(:base_shipping_method, require_ship_address: true), + selected: true + ) + end + + before do + delivery_shipment.shipping_rates << delivery_shipping_rate + order.shipments << delivery_shipment + end + it "populates the shipping address" do order_updater.before_save_hook expect(order.ship_address.firstname).to eq(distributor.address.firstname) end end - context 'when the shipping method requires a delivery address' do + context 'when any of the shipping methods requires a delivery address' do let(:shipping_method) { build(:base_shipping_method, require_ship_address: true) } let(:address) { build(:address, firstname: 'will') }