Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

fix: allow guest checkout when order is created by logged in user (#12644)

Caio Câmara64675e851fb3
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 59195d1d9e..d18255c0f9 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -91,6 +91,8 @@ module Spree
acts_as_taggable_on :tags
acts_as_taggable_tenant :store_id
+ ASSOCIATED_USER_ATTRIBUTES = [:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id]
+
belongs_to :user, class_name: "::#{Spree.user_class}", optional: true, autosave: true
belongs_to :created_by, class_name: "::#{Spree.admin_user_class}", optional: true
belongs_to :approver, class_name: "::#{Spree.admin_user_class}", optional: true
@@ -355,7 +357,7 @@ module Spree
self.bill_address ||= user.bill_address
self.ship_address ||= user.ship_address
- changes = slice(:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id)
+ changes = slice(*ASSOCIATED_USER_ATTRIBUTES)
# immediately persist the changes we just made, but don't use save
# since we might have an invalid address associated
@@ -364,6 +366,12 @@ module Spree
end
end
+ def disassociate_user!
+ nullified_attributes = ASSOCIATED_USER_ATTRIBUTES.index_with(nil)
+
+ update!(nullified_attributes)
+ end
+
def quantity_of(variant, options = {})
line_item = find_line_item_by_variant(variant, options)
line_item ? line_item.quantity : 0
diff --git a/core/app/models/spree/order_merger.rb b/core/app/models/spree/order_merger.rb
index 3951b99718..d7fa6c679c 100644
--- a/core/app/models/spree/order_merger.rb
+++ b/core/app/models/spree/order_merger.rb
@@ -7,7 +7,7 @@ module Spree
@order = order
end
- def merge!(other_order, user = nil)
+ def merge!(other_order, user = nil, discard_merged: true)
other_order.line_items.each do |other_order_line_item|
next unless other_order_line_item.currency == order.currency
@@ -16,12 +16,14 @@ module Spree
end
set_user(user)
- clear_addresses(other_order)
+ clear_addresses(other_order) if discard_merged
persist_merge
- # So that the destroy doesn't take out line items which may have been re-assigned
- other_order.line_items.reload
- other_order.destroy
+ if discard_merged
+ # So that the destroy doesn't take out line items which may have been re-assigned
+ other_order.line_items.reload
+ other_order.destroy
+ end
end
# Compare the line item of the other order with mine.
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 26b9e4d0f9..b0df158b7a 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -795,6 +795,7 @@ en:
continue_as_guest: Continue as a guest
continue_selling_when_out_of_stock: Continue selling when out of stock
continue_shopping: Continue shopping
+ continue_without_logging_in: Continue without logging in
copy: Copy
copy_id: Copy ID
copy_link: Copy link
diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb
index a52033573f..8aaf0d28ef 100644
--- a/core/spec/models/spree/order_merger_spec.rb
+++ b/core/spec/models/spree/order_merger_spec.rb
@@ -14,6 +14,19 @@ module Spree
expect { order_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
+ context 'when `discard_merged` is false' do
+ it 'keeps the other order' do
+ subject.merge!(order_2, discard_merged: false)
+ expect { order_2.reload }.not_to raise_error
+ end
+
+ it 'does not change the other order' do
+ expect {
+ subject.merge!(order_2, discard_merged: false)
+ }.not_to change(order_2, :attributes)
+ end
+ end
+
it 'persist the merge' do
expect(subject).to receive(:persist_merge)
subject.merge!(order_2)
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 917273e953..2923ef7660 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -838,6 +838,28 @@ describe Spree::Order, type: :model do
end
end
+ describe '#disassociate_user!' do
+ let(:order) { create(:order_with_line_items) }
+ let(:expected_order_attributes) {
+ {
+ user: nil,
+ user_id: nil,
+ email: nil,
+ created_by: nil,
+ created_by_id: nil,
+ bill_address: nil,
+ bill_address_id: nil,
+ ship_address: nil,
+ ship_address_id: nil
+ }
+ }
+
+ it 'disassociates a user from an order' do
+ order.disassociate_user!
+ expect(order).to have_attributes(expected_order_attributes)
+ end
+ end
+
describe '#can_ship?' do
let(:order) { Spree::Order.create }
diff --git a/storefront/app/controllers/spree/checkout_controller.rb b/storefront/app/controllers/spree/checkout_controller.rb
index 700f1070b6..e70cd19f8e 100644
--- a/storefront/app/controllers/spree/checkout_controller.rb
+++ b/storefront/app/controllers/spree/checkout_controller.rb
@@ -22,7 +22,7 @@ module Spree
before_action :remove_expired_gift_card, only: :edit
- before_action :store_location, only: :edit
+ before_action :store_user_location, only: :edit
after_action :clean_analytics_session, only: :edit
after_action :clear_checkout_completed_session, only: [:complete]
@@ -152,6 +152,14 @@ module Spree
private
+ def store_user_location
+ if try_spree_current_user
+ store_location
+ else
+ store_location(spree.cart_path)
+ end
+ end
+
# rather then using cookies like in old Spree we're going to fetch the order based on the
# token passed in the parameters, this allows us to share carts, send payment links, etc
def load_order
@@ -179,8 +187,23 @@ module Spree
flash[:error] = 'You cannot access this checkout'
redirect_to_cart
elsif try_spree_current_user.nil? && [email protected]?
- store_location
- redirect_to spree_login_path
+ if params[:guest] && current_store.prefers_guest_checkout?
+ @order = current_store
+ .orders
+ .create!(current_order_params.except(:token, :user_id))
+ .tap do |order|
+ order.merge!(@order, discard_merged: false)
+ order.disassociate_user!
+ end
+
+ reset_session
+ cookies.permanent.signed[:token] = @order.token
+
+ redirect_to spree.checkout_path(@order.token)
+ else
+ store_location
+ redirect_to spree_login_path
+ end
end
end
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index 832c4a4688..d94f56bc1c 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -30,6 +30,8 @@ module Spree
:storefront_products_scope, :storefront_products,
:default_products_sort, :default_products_finder_params
+ helper_method :stored_location
+
before_action :redirect_to_default_locale
before_action :render_404_if_store_not_exists
rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_authenticity_token
@@ -219,6 +221,13 @@ module Spree
store_location_for(Devise.mappings.keys.first, location)
end
+ def stored_location
+ return unless defined?(after_sign_in_path_for)
+ return unless defined?(Devise)
+
+ after_sign_in_path_for(Devise.mappings.keys.first)
+ end
+
def redirect_to_cart
redirect_to spree.cart_path
end
diff --git a/storefront/app/views/devise/sessions/new.html.erb b/storefront/app/views/devise/sessions/new.html.erb
index 2249cb5a91..06012e81cd 100644
--- a/storefront/app/views/devise/sessions/new.html.erb
+++ b/storefront/app/views/devise/sessions/new.html.erb
@@ -21,5 +21,21 @@
</div>
<% end %>
<%= render "devise/shared/links" %>
+
+ <% if current_order.present? &&
+ current_store&.prefers_guest_checkout? &&
+ stored_location&.include?(spree.checkout_path(current_order.token))
+ %>
+ <div class="relative my-5 flex items-center">
+ <div class="flex-grow border-t border-gray-200"></div>
+ <span class="mx-4 text-sm text-gray-500 uppercase"><%= Spree.t(:or) %></span>
+ <div class="flex-grow border-t border-gray-200"></div>
+ </div>
+
+ <div class="flex flex-col items-center gap-4">
+ <p class="text-sm text-gray-600"><%= Spree.t(:continue_without_logging_in) %></p>
+ <%= link_to Spree.t(:continue_as_guest), spree.checkout_path(current_order.token, guest: true), class: 'btn btn-secondary w-full max-w-sm', data: { turbo: false } %>
+ </div>
+ <% end %>
</div>
<% end %>
diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rb
index 04bc275893..87297030ad 100644
--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb
+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb
@@ -130,6 +130,20 @@ describe Spree::CheckoutController, type: :controller do
get :edit, params: { token: order.token }
expect(response).to redirect_to('/login')
end
+
+ context 'when guest checkout is allowed' do
+ let(:allow_guest_checkout) { true }
+
+ it 'creates a new order and allows access to the checkout' do
+ expect {
+ get :edit, params: { token: order.token, guest: true }
+ }.to change(Spree::Order, :count).by(1)
+
+ new_order = Spree::Order.last
+
+ expect(response).to redirect_to(spree.checkout_path(new_order.token))
+ end
+ end
end
context 'when user is logged in' do