diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index 1fb8649a3f..cbb7c4e41c 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -141,6 +141,10 @@ class SplitCheckoutController < ::BaseController def update_order return if params[:confirm_order] || @order.errors.any? + # If we have "pick up" shipping method (require_ship_address is set to false), use the + # distributor address as shipping address + use_shipping_address_from_distributor if shipping_method_ship_address_not_required? + @order.select_shipping_method(params[:shipping_method_id]) @order.update(order_params) @order.updater.update_totals_and_states @@ -150,6 +154,29 @@ class SplitCheckoutController < ::BaseController @order.errors.empty? end + def use_shipping_address_from_distributor + @order.ship_address = @order.address_from_distributor + + # Add the missing data + bill_address = params[:order][:bill_address_attributes] + @order.ship_address.firstname = bill_address[:firstname] + @order.ship_address.lastname = bill_address[:lastname] + @order.ship_address.phone = bill_address[:phone] + + # Remove shipping address from parameter so we don't override the address we just set + params[:order].delete(:ship_address_attributes) + end + + def shipping_method_ship_address_not_required? + selected_shipping_method = allowed_shipping_methods&.select do |sm| + sm.id.to_s == params[:shipping_method_id] + end + + return false if selected_shipping_method.empty? + + selected_shipping_method.first.require_ship_address == false + end + def summary_step? params[:step] == "summary" end diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index b293aff3df..d1a48b8d2e 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -458,6 +458,19 @@ describe "As a consumer, I want to checkout my order" do fill_notes("SpEcIaL NoTeS") proceed_to_payment end + + context 'when the user has no shipping address' do + before do + # Hack so we can have "Shipping address same as billing address?" unticked + choose free_shipping_with_required_address.name + uncheck "Shipping address same as billing address?" + choose free_shipping_without_required_address.name + end + + it "redirects the user to the Payment Method step" do + proceed_to_payment + end + end end describe "selecting a delivery method with a shipping fee" do