diff --git a/app/controllers/spree/admin/orders/customer_details_controller.rb b/app/controllers/spree/admin/orders/customer_details_controller.rb index 2a1f7a2595..0ce7ccc166 100644 --- a/app/controllers/spree/admin/orders/customer_details_controller.rb +++ b/app/controllers/spree/admin/orders/customer_details_controller.rb @@ -65,6 +65,7 @@ module Spree params.require(:order).permit( :email, :use_billing, + :customer_id, bill_address_attributes: ::PermittedAttributes::Address.attributes, ship_address_attributes: ::PermittedAttributes::Address.attributes ) diff --git a/app/views/spree/admin/orders/customer_details/edit.html.haml b/app/views/spree/admin/orders/customer_details/edit.html.haml index d84b1fcdc5..fe68760d93 100644 --- a/app/views/spree/admin/orders/customer_details/edit.html.haml +++ b/app/views/spree/admin/orders/customer_details/edit.html.haml @@ -24,3 +24,4 @@ = form_for @order, :url => admin_order_customer_url(@order) do |f| = render 'form', :f => f + = f.hidden_field :customer_id, value: @order.customer_id, id: "customer_id" diff --git a/app/webpacker/controllers/select_customer_controller.js b/app/webpacker/controllers/select_customer_controller.js index 7034dbd4d0..98df1ecddd 100644 --- a/app/webpacker/controllers/select_customer_controller.js +++ b/app/webpacker/controllers/select_customer_controller.js @@ -53,6 +53,7 @@ export default class extends TomSelectController { }); $("#order_email").val(customer.email); $("#user_id").val(customer.user_id); + $("#customer_id").val(customer.id); } setValueOnTomSelectController = (element, value) => { diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index ac21aec973..59cdb69c5c 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -143,6 +143,37 @@ describe ' end end + context "when creating an order with a customer-only" do + let(:customer2) { create(:customer, enterprise: distributor) } + let(:customer3) { create(:customer, enterprise: distributor) } + + before do + login_as_admin + visit spree.new_admin_order_path + select2_select distributor.name, from: 'order_distributor_id' + select2_select order_cycle.name, from: 'order_order_cycle_id' + click_button 'Next' + expect(Spree::Order.last.customer_id).to be_nil + tomselect_search_and_select customer2.email, from: 'customer_search_override' + check 'order_use_billing' + click_button 'Update' + expect(page).to have_content 'Customer Details updated' + end + + it "set the right customer attached to the order" do + expect(Spree::Order.last.reload.customer).to eq customer2 + end + + it "when changing the attached customer,"\ + "it should update the order customer (not only its details)" do + tomselect_search_and_select customer3.email, from: 'customer_search_override' + expect do + click_button 'Update' + expect(page).to have_field 'order_email', with: customer3.email + end.to change { Spree::Order.last.reload.customer }.from(customer2).to(customer3) + end + end + it "can add a product to an existing order" do login_as_admin visit spree.edit_admin_order_path(order)