From 72f5b1b251c62df5b2a2ab71860204d3380dbed4 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 19 Aug 2020 17:36:36 +0100 Subject: [PATCH] Revert "Remove unreachable order recovery code" This reverts commit 355c5f5c55ceffce9ffb958ea72be8f33eb89e97. This code is necessary to preserver cart contents across logins on different browser sessions. --- lib/spree/core/controller_helpers/order.rb | 13 +++++++++++-- spec/controllers/base_controller_spec.rb | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/spree/core/controller_helpers/order.rb b/lib/spree/core/controller_helpers/order.rb index fe99381c7f..e95499bef2 100644 --- a/lib/spree/core/controller_helpers/order.rb +++ b/lib/spree/core/controller_helpers/order.rb @@ -68,9 +68,18 @@ module Spree session[:guest_token] = nil end - # Load current order and create a new one if necessary. + # Recover incomplete orders from other sessions after logging in. def set_current_order - current_order(true) if spree_current_user + return unless (user = spree_current_user) + + last_incomplete_order = user.last_incomplete_spree_order + + if session[:order_id].nil? && last_incomplete_order + session[:order_id] = last_incomplete_order.id + end + + # Load current order and create a new one if necessary. + current_order(true) end def current_currency diff --git a/spec/controllers/base_controller_spec.rb b/spec/controllers/base_controller_spec.rb index e68e10e0c2..fb1d399335 100644 --- a/spec/controllers/base_controller_spec.rb +++ b/spec/controllers/base_controller_spec.rb @@ -28,6 +28,17 @@ describe BaseController, type: :controller do expect(user.orders.count).to eq 1 end + it "uses the last incomplete order" do + last_cart = create(:order, user: user, created_by: user, state: "cart", completed_at: nil) + allow(controller).to receive(:spree_current_user).and_return(user) + + expect { + get :index + }.to_not change { Spree::Order.count } + + expect(session[:order_id]).to eq last_cart.id + end + it "ignores the last incomplete order" do # Spree used to merge the last order with the current one. # And we used to override that logic to delete old incomplete orders.