diff --git a/admin/app/components/solidus_admin/orders/index/component.rb b/admin/app/components/solidus_admin/orders/index/component.rb index 5c412db937e..08929335f19 100644 --- a/admin/app/components/solidus_admin/orders/index/component.rb +++ b/admin/app/components/solidus_admin/orders/index/component.rb @@ -143,7 +143,7 @@ def customer_column col: { class: "w-[400px]" }, header: :customer, data: ->(order) do - customer_email = order.user&.email + customer_email = order.order_email content_tag :div, String(customer_email) end } diff --git a/admin/spec/features/orders/index_spec.rb b/admin/spec/features/orders/index_spec.rb index ddc8f83f5ff..e0df6d7bb1c 100644 --- a/admin/spec/features/orders/index_spec.rb +++ b/admin/spec/features/orders/index_spec.rb @@ -6,11 +6,12 @@ before { sign_in create(:admin_user, email: 'admin@example.com') } it "lists products", :js do - create(:order, number: "R123456789", total: 19.99) + create(:order, number: "R123456789", total: 19.99, email: "customer@example.com") visit "/admin/orders" click_on "In Progress" + expect(page).to have_content("customer@example.com") expect(page).to have_content("R123456789") expect(page).to have_content("$19.99") expect(page).to be_axe_clean diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 5b04d0009c8..2cf3e5334fc 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -726,6 +726,10 @@ def validate_payments_attributes(attributes) end end + def order_email + email.presence || user&.email + end + private def process_payments_before_complete diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index eef3df40c5a..23ab7f86500 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -1832,4 +1832,26 @@ def validate(line_item) expect { subject }.to change { Spree::OrderPromotion.count }.from(1).to(0) end end + + describe "order_email" do + context "when a new order is created" do + it "returns nil" do + expect(Spree::Order.new.order_email).to eq(nil) + end + end + + context "when a logged in user creates an order" do + it "returns the customer's email" do + order = build(:order, user: build(:user, email: "customer@example.com")) + expect(order.order_email).to eq("customer@example.com") + end + end + + context "when order email is different than user's email" do + it "gives preference to order's email" do + order = build(:order, email: "buyer@example.com", user: build(:user, email: "customer@example.com")) + expect(order.order_email).to eq("buyer@example.com") + end + end + end end