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.

Add settings for allowing guest checkout in the Settings -> Checkout (#12560)

Łukasz Adamczyk590a4b315642
diff --git a/admin/app/views/spree/admin/stores/form/_checkout.html.erb b/admin/app/views/spree/admin/stores/form/_checkout.html.erb
index cafee994a6..6f3c7a4f6c 100644
--- a/admin/app/views/spree/admin/stores/form/_checkout.html.erb
+++ b/admin/app/views/spree/admin/stores/form/_checkout.html.erb
@@ -1,27 +1,49 @@
<%= content_for(:page_title) do %>
- Checkout settings
+ <%= Spree.t(:checkout) %> <%= Spree.t(:settings) %>
<% end %>
<%= content_for :page_alerts do %>
<div class="alert alert-info mb-3">
<p>
- Checkout footer links are added automatically based on the <%= link_to 'Policies', spree.edit_admin_store_path + '?section=policies', class: 'alert-link' %> content.
+ <%= Spree.t('admin.checkout_settings.policy_copy', policies_link: link_to(Spree.t(:policies), spree.edit_admin_store_path + '?section=policies', class: 'alert-link')).html_safe %>
</p>
</div>
<% end %>
-<div class="card-lg">
- <h5 class="mb-3"><%= Spree.t(:checkout_message) %></h5>
+<div class="row">
+ <div class="col-lg-7">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:checkout_message) %></h5>
+ </div>
- <div class="row">
- <div class="col-lg-4">
- <p class="text-muted">
- Visible to your customers on the checkout page in the right sidebar.
- </p>
+ <div class="card-body">
+ <p class="text-muted">
+ <%= Spree.t('admin.checkout_settings.checkout_message_description') %>
+ </p>
+ <div class="trix-container">
+ <%= f.rich_text_area :checkout_message, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
</div>
- <div class="col-lg-7 offset-lg-1">
- <div class="trix-container">
- <%= f.rich_text_area :checkout_message, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+
+ <div class="col-lg-5">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:guest_checkout) %></h5>
+ </div>
+
+ <div class="card-body">
+ <p class="text-muted">
+ <%= Spree.t('admin.checkout_settings.guest_checkout.description') %>
+ </p>
+
+ <div class="custom-control custom-checkbox mb-3">
+ <%= f.check_box :preferred_guest_checkout, class: "custom-control-input" %>
+ <%= f.label :preferred_guest_checkout, Spree.t('admin.checkout_settings.guest_checkout.label'), class: "custom-control-label" %>
+ <%= f.error_message_on :preferred_guest_checkout %>
+ </div>
</div>
</div>
</div>
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index 2086e6f169..cf51051772 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -39,6 +39,12 @@ en:
title:
add_tags: Add tags
remove_tags: Remove tags
+ checkout_settings:
+ checkout_message_description: Visible to your customers on the checkout page in the right sidebar.
+ guest_checkout:
+ description: Allow customers to checkout without creating an account.
+ label: Allow guest checkout
+ policy_copy: Checkout footer links are added automatically based on the %{policies_link} content.
copy_payment_link: Copy Payment Link
copy_to_clipboard: Copy to clipboard
coupon_codes:
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 178baa6c49..9f5551d260 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -141,7 +141,8 @@ module Spree
:preferred_digital_asset_link_expire_time,
:logo, :mailer_logo, :social_logo, :favicon_image,
:import_products_from_store_id, :import_payment_methods_from_store_id,
- :checkout_message, :customer_terms_of_service, :customer_privacy_policy,
+ :checkout_message, :preferred_guest_checkout,
+ :customer_terms_of_service, :customer_privacy_policy,
:customer_returns_policy, :customer_shipping_policy, :default_country_iso]
@@store_credit_attributes = %i[amount currency category_id memo]
diff --git a/storefront/app/controllers/spree/checkout_controller.rb b/storefront/app/controllers/spree/checkout_controller.rb
index cbd85f5f82..97a7e033f3 100644
--- a/storefront/app/controllers/spree/checkout_controller.rb
+++ b/storefront/app/controllers/spree/checkout_controller.rb
@@ -4,6 +4,7 @@ module Spree
include Spree::CheckoutHelper
include Spree::CheckoutAnalyticsHelper
+ before_action :require_user, unless: -> { current_store.prefers_guest_checkout? }
before_action :load_order
before_action :remove_out_of_stock_items, only: [:edit, :update]
diff --git a/storefront/app/controllers/spree/line_items_controller.rb b/storefront/app/controllers/spree/line_items_controller.rb
index d4e39a4abb..e62791e187 100644
--- a/storefront/app/controllers/spree/line_items_controller.rb
+++ b/storefront/app/controllers/spree/line_items_controller.rb
@@ -2,7 +2,6 @@ module Spree
class LineItemsController < Spree::StoreController
include CartMethods
- before_action :require_user, unless: -> { current_store.prefers_guest_checkout? }
before_action :load_variant, only: :create
before_action :assign_order_with_lock, except: :create
before_action :load_line_item, except: :create
diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rb
index d5a3f26235..04bc275893 100644
--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb
+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb
@@ -67,6 +67,32 @@ describe Spree::CheckoutController, type: :controller do
get :edit, params: { token: order.token, state: 'address' }
end
+ context 'when user is not signed in' do
+ let(:user) { nil }
+
+ before do
+ allow(store).to receive(:prefers_guest_checkout?).and_return(allow_guest_checkout)
+ end
+
+ context 'when guest checkout is allowed' do
+ let(:allow_guest_checkout) { true }
+
+ it 'allows access to the checkout' do
+ get :edit, params: { token: order.token }
+ expect(response).to redirect_to(spree.checkout_state_path(order.token, 'address'))
+ end
+ end
+
+ context 'when guest checkout is not allowed' do
+ let(:allow_guest_checkout) { false }
+
+ it 'redirects to the login page' do
+ get :edit, params: { token: order.token }
+ expect(response).to redirect_to('/login')
+ end
+ end
+ end
+
context 'when entering the checkout' do
context 'when user is signed in' do
let(:user) { create(:user) }