diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index a0aed03943..997ef5e5bf 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -60,6 +60,9 @@ class SplitCheckoutController < ::BaseController def order_error_messages # Remove ship_address.* errors if no shipping method is not selected remove_ship_address_errors if @order.errors[:shipping_method].present? + # Reorder errors to make sure the most important ones are shown first + # and finally, return the error messages to sentence + reorder_errors.map(&:full_message).to_sentence end def remove_ship_address_errors @@ -71,6 +74,28 @@ class SplitCheckoutController < ::BaseController @order.errors.delete("ship_address.zipcode") end + def reorder_errors + @order.errors.sort_by do |e| + case e.attribute + when /email/i then 0 + when /phone/i then 1 + when /bill_address/i then 2 + bill_address_error_order(e) + else 20 + end + end + end + + def bill_address_error_order(error) + case error.attribute + when /firstname/i then 0 + when /lastname/i then 1 + when /address1/i then 2 + when /city/i then 3 + when /zipcode/i then 4 + else 5 + end + end + def flash_error_when_no_shipping_method_available flash[:error] = I18n.t('split_checkout.errors.no_shipping_methods_available') end diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index 0da3fe9135..299ffaceef 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -203,6 +203,17 @@ describe "As a consumer, I want to checkout my order" do expect(page).not_to have_content "Bill address phone can't be blank" expect(page).to have_content "Customer phone can't be blank" end + + context "with no email filled in" do + before do + fill_in "Email", with: "" + click_button "Next - Payment method" + end + + it "should display error message in the right order" do + expect(page).to have_content "Customer E-Mail can't be blank, Customer E-Mail is invalid, Customer phone can't be blank, Bill address firstname can't be blank, Bill address lastname can't be blank, Bill address address1 can't be blank, Bill address city can't be blank, Bill address zipcode can't be blank, and Shipping method Select a shipping method" + end + end end it "should allow visit '/checkout/details'" do