Skip to content

Commit

Permalink
When creating Order, correctly assign ship_address or use_billing
Browse files Browse the repository at this point in the history
The Order should use_billing == true if ship_address is missing, is empty, or is the same as bill_address
  • Loading branch information
mrbrdo committed May 12, 2022
1 parent b835982 commit 0fa0dbe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def states
before_validation :ensure_currency_presence

before_validation :clone_billing_address, if: :use_billing?
attr_accessor :use_billing
attr_writer :use_billing

before_create :create_token
before_create :link_by_email
Expand Down Expand Up @@ -299,6 +299,9 @@ def associate_user!(user, override_email = true)
self.created_by ||= user
self.bill_address_id ||= user.bill_address_id
self.ship_address_id ||= user.ship_address_id
if ship_address_id == bill_address_id
self.ship_address_id = nil
end

changes = slice(:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id)

Expand Down Expand Up @@ -557,10 +560,6 @@ def refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_F
shipments.map { |s| s.refresh_rates(shipping_method_filter) }
end

def shipping_eq_billing_address?
bill_address == ship_address
end

def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
Expand Down Expand Up @@ -689,6 +688,12 @@ def has_free_shipping?
spree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).exists?
end

def use_billing
@use_billing.in?([true, 'true', '1']) ||
ship_address.nil? || ship_address.empty? ||
ship_address == bill_address
end

private

def link_by_email
Expand Down Expand Up @@ -732,7 +737,7 @@ def after_resume
end

def use_billing?
use_billing.in?([true, 'true', '1'])
use_billing
end

def ensure_currency_presence
Expand Down
8 changes: 4 additions & 4 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1228,11 +1228,11 @@ def attributes(amount = 0)
end
end

describe '#shipping_eq_billing_address' do
describe '#use_billing' do
let!(:order) { create(:order) }

context 'with only bill address' do
it { expect(order.shipping_eq_billing_address?).to eq(false) }
it { expect(order.use_billing).to eq(true) }
end

context 'blank addresses' do
Expand All @@ -1241,7 +1241,7 @@ def attributes(amount = 0)
order.ship_address = Spree::Address.new
end

it { expect(order.shipping_eq_billing_address?).to eq(true) }
it { expect(order.use_billing).to eq(true) }
end

context 'no addresses' do
Expand All @@ -1250,7 +1250,7 @@ def attributes(amount = 0)
order.ship_address = nil
end

it { expect(order.shipping_eq_billing_address?).to eq(true) }
it { expect(order.use_billing).to eq(true) }
end
end

Expand Down

0 comments on commit 0fa0dbe

Please sign in to comment.