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.

Support creating user accounts during checkout when user wants to (#12290)

Damian Legawiec29edbaa7c1e6
diff --git a/core/app/models/spree/legacy_user.rb b/core/app/models/spree/legacy_user.rb
index fe8e63f0a9..9b7f00a65e 100644
--- a/core/app/models/spree/legacy_user.rb
+++ b/core/app/models/spree/legacy_user.rb
@@ -8,6 +8,6 @@ module Spree
self.table_name = 'spree_users'
- attr_accessor :password, :password_confirmation, :first_name, :last_name
+ attr_accessor :password, :password_confirmation
end
end
diff --git a/core/app/models/spree/order/checkout.rb b/core/app/models/spree/order/checkout.rb
index bb467f8dff..c49c3ac4bb 100644
--- a/core/app/models/spree/order/checkout.rb
+++ b/core/app/models/spree/order/checkout.rb
@@ -88,6 +88,7 @@ module Spree
order.create_digital_links if order.some_digital?
end
+ after_transition to: :complete, do: :create_user_record
after_transition to: :complete, do: :persist_user_credit_card
before_transition to: :payment, do: :set_shipments_cost
before_transition to: :payment, do: :create_tax_charge!
@@ -270,6 +271,20 @@ module Spree
end
end
+ def create_user_record
+ return if user.present?
+ return unless signup_for_an_account?
+
+ result = Spree::Orders::CreateUserAccount.call(order: self, accepts_email_marketing: accept_marketing?)
+
+ if result.success?
+ self.user = result.value
+ save
+
+ user.send_welcome_email if user.respond_to?(:send_welcome_email)
+ end
+ end
+
def persist_user_credit_card
return unless !temporary_credit_card && user_id && valid_credit_cards.present?
diff --git a/core/app/services/spree/orders/create_user_account.rb b/core/app/services/spree/orders/create_user_account.rb
new file mode 100644
index 0000000000..67f2aadaf1
--- /dev/null
+++ b/core/app/services/spree/orders/create_user_account.rb
@@ -0,0 +1,65 @@
+# this service creates a user account when someone places an order
+# and checks the box to create an account
+module Spree
+ module Orders
+ class CreateUserAccount
+ prepend ::Spree::ServiceModule::Base
+
+ def call(order:, accepts_email_marketing: false)
+ existing_user = Spree.user_class.find_by(email: order.email)
+ return existing_user if existing_user.present?
+
+ user = create_new_user(order, accepts_email_marketing)
+ return failure(:user_creation_failed) unless user.persisted?
+
+ assign_ship_address(order, user)
+ assign_bill_address(order, user)
+
+ # assign newly created user to the order
+ order.update_columns(user_id: user.id, updated_at: Time.current)
+
+ success(user.reload)
+ end
+
+ private
+
+ def create_new_user(order, accepts_email_marketing = false)
+ firstname = order.bill_address&.firstname || order.ship_address&.firstname
+ lastname = order.bill_address&.lastname || order.ship_address&.lastname
+ phone = order.bill_address&.phone || order.ship_address&.phone
+
+ # we need to generate a password for the user
+ password = SecureRandom.base58(16)
+
+ user = Spree.user_class.new
+ user.email = order.email
+ user.first_name = firstname if user.respond_to?(:first_name)
+ user.last_name = lastname if user.respond_to?(:last_name)
+ user.phone = phone if user.respond_to?(:phone)
+ user.accepts_email_marketing = accepts_email_marketing.to_b if user.respond_to?(:accepts_email_marketing)
+ user.password = password if user.respond_to?(:password)
+ user.password_confirmation = password if user.respond_to?(:password_confirmation)
+
+ user.save
+
+ user
+ end
+
+ def assign_bill_address(order, user)
+ if order.bill_address.present?
+ order.bill_address.update_columns(user_id: user.id, updated_at: Time.current)
+
+ user.update_columns(bill_address_id: order.bill_address_id, updated_at: Time.current) unless user.bill_address_id.present?
+ end
+ end
+
+ def assign_ship_address(order, user)
+ if order.ship_address.present?
+ order.ship_address.update_columns(user_id: user.id, updated_at: Time.current)
+
+ user.update_columns(ship_address_id: order.ship_address_id, updated_at: Time.current) unless user.ship_address_id.present?
+ end
+ end
+ end
+ end
+end
diff --git a/core/db/migrate/20250107220432_add_accept_marketing_and_signup_for_an_account_to_spree_orders.rb b/core/db/migrate/20250107220432_add_accept_marketing_and_signup_for_an_account_to_spree_orders.rb
new file mode 100644
index 0000000000..e3b4e754be
--- /dev/null
+++ b/core/db/migrate/20250107220432_add_accept_marketing_and_signup_for_an_account_to_spree_orders.rb
@@ -0,0 +1,6 @@
+class AddAcceptMarketingAndSignupForAnAccountToSpreeOrders < ActiveRecord::Migration[6.1]
+ def change
+ add_column :spree_orders, :accept_marketing, :boolean, default: false, if_not_exists: true
+ add_column :spree_orders, :signup_for_an_account, :boolean, default: false, if_not_exists: true
+ end
+end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 3a77caf14c..712b87aca1 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -49,7 +49,7 @@ module Spree
@@checkout_attributes = [
:coupon_code, :email, :shipping_method_id, :special_instructions, :use_billing, :use_shipping,
- :user_id, :bill_address_id, :ship_address_id
+ :user_id, :bill_address_id, :ship_address_id, :accept_marketing, :signup_for_an_account
]
@@classification_attributes = [
diff --git a/core/spec/models/spree/order/checkout_spec.rb b/core/spec/models/spree/order/checkout_spec.rb
index bb4a91863f..8a43043b93 100644
--- a/core/spec/models/spree/order/checkout_spec.rb
+++ b/core/spec/models/spree/order/checkout_spec.rb
@@ -484,6 +484,36 @@ describe Spree::Order, type: :model do
order.next!
expect(order.user.reload.default_credit_card).to be_nil
end
+
+ context 'when user is not present' do
+ before do
+ order.user = nil
+ order.email = '[email protected]'
+ order.save!
+ end
+
+ context 'with signup_for_an_account set to true' do
+ before do
+ allow(order).to receive(:signup_for_an_account?).and_return(true)
+ end
+
+ it 'creates a new user' do
+ expect { order.next! }.to change { Spree.user_class.count }.by(1)
+ expect(order.user).to be_present
+ expect(order.user.email).to eq(order.email)
+ end
+ end
+
+ context 'with signup_for_an_account set to false' do
+ before do
+ allow(order).to receive(:signup_for_an_account?).and_return(false)
+ end
+
+ it 'does not create a new user' do
+ expect { order.next! }.not_to change { Spree.user_class.count }
+ end
+ end
+ end
end
end
diff --git a/core/spec/services/spree/orders/create_user_account_spec.rb b/core/spec/services/spree/orders/create_user_account_spec.rb
new file mode 100644
index 0000000000..014bd842d6
--- /dev/null
+++ b/core/spec/services/spree/orders/create_user_account_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Spree::Orders::CreateUserAccount do
+ let(:store) { Spree::Store.default }
+ let(:address) { create(:address, country: store.default_country, firstname: 'John', lastname: 'Snow') }
+ let(:order) do
+ create(:completed_order_with_totals, bill_address: address, ship_address: address, store: store, user: nil, email: '[email protected]')
+ end
+
+ subject { described_class.call(order: order) }
+
+ context 'when order has no user' do
+ let(:new_user) { Spree.user_class.find_by!(email: order.email) }
+
+ it 'creates a new user' do
+ expect { subject }.to change { Spree.user_class.count }.by(1)
+
+ expect(new_user.email).to eq(order.email)
+ expect(new_user.first_name).to eq(order.bill_address.firstname)
+ expect(new_user.last_name).to eq(order.bill_address.lastname)
+ end
+
+ it 'assigns the ship address to the user' do
+ subject
+ expect(new_user.ship_address).to eq(order.ship_address)
+ end
+
+ it 'assigns the bill address to the user' do
+ subject
+ expect(new_user.bill_address).to eq(order.bill_address)
+ end
+
+ it 'assigns the user to the order' do
+ subject
+ expect(order.reload.user).to be_present
+ expect(order.user).to eq(new_user)
+ end
+ end
+
+ context 'when user with the given email already exists' do
+ let!(:user) { create(:user, email: '[email protected]') }
+
+ it 'does not create a new user' do
+ expect { subject }.to change { Spree.user_class.count }.by(0)
+ end
+ end
+end