Merge pull request #9272 from jibees/7905-retrieve-order-already-placed-on-user-confirmation-with-customer-email

On user confirmation, retrieve and link all orders to the new user that were already placed with the same email
This commit is contained in:
Filipe
2022-06-21 18:12:58 +01:00
committed by GitHub
2 changed files with 12 additions and 1 deletions

View File

@@ -41,7 +41,7 @@ module Spree
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
after_create :associate_customers
after_create :associate_customers, :associate_orders
validate :limit_owned_enterprises
@@ -113,6 +113,12 @@ module Spree
self.customers = Customer.where(email: email)
end
def associate_orders
Spree::Order.where(customer: customers).find_each do |order|
order.associate_user!(self)
end
end
def can_own_more_enterprises?
owned_enterprises.reload.size < enterprise_limit
end

View File

@@ -100,6 +100,7 @@ describe Spree::User do
let(:enterprise2) { create(:enterprise) }
let!(:customer1) { create(:customer, user: nil, email: email, enterprise: enterprise1) }
let!(:customer2) { create(:customer, user: nil, email: email, enterprise: enterprise2) }
let!(:order) { create(:order, customer: customer1) }
let!(:user) { create(:user, email: email) }
it "should associate these customers with the created user" do
@@ -107,6 +108,10 @@ describe Spree::User do
expect(user.customer_of(enterprise1)).to be_truthy
expect(user.customer_of(enterprise2)).to be_truthy
end
it "should associate the orders passed by customer linked to the created user" do
expect(user.orders.reload).to include order
end
end
end