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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add possibility to configure address company and order special instructions fields (#12789)
Łukasz Adamczyk5d24ed88596f
diff --git a/admin/app/views/spree/admin/orders/_special_instructions.html.erb b/admin/app/views/spree/admin/orders/_special_instructions.html.erbnew file mode 100644index 0000000000..973e371c6d--- /dev/null+++ b/admin/app/views/spree/admin/orders/_special_instructions.html.erb@@ -0,0 +1,14 @@+<% if @order.special_instructions.present? %>+ <div class="card mb-4">+ <div class="card-header">+ <h5 class="card-title">+ <%= I18n.t('activerecord.attributes.spree/order.special_instructions') %>+ </h5>+ </div>+ <div class="card-body">+ <div class="card-text">+ <%= simple_format(@order.special_instructions) %>+ </div>+ </div>+ </div>+<% end %>diff --git a/admin/app/views/spree/admin/orders/edit.html.erb b/admin/app/views/spree/admin/orders/edit.html.erbindex 5a70f40692..936be13fc6 100644--- a/admin/app/views/spree/admin/orders/edit.html.erb+++ b/admin/app/views/spree/admin/orders/edit.html.erb@@ -15,6 +15,7 @@</div><div class="col-lg-4"><%= render 'customer' %>+ <%= render 'special_instructions' %><%= render_admin_partials(:order_page_sidebar_partials, order: @order) %></div></div>diff --git a/admin/app/views/spree/admin/stores/form/_checkout.html.erb b/admin/app/views/spree/admin/stores/form/_checkout.html.erbindex 6f3c7a4f6c..2f9b59e9b0 100644--- a/admin/app/views/spree/admin/stores/form/_checkout.html.erb+++ b/admin/app/views/spree/admin/stores/form/_checkout.html.erb@@ -31,18 +31,38 @@<div class="col-lg-5"><div class="card mb-4"><div class="card-header">- <h5 class="card-title"><%= Spree.t(:guest_checkout) %></h5>+ <h5 class="card-title"><%= Spree.t(:settings) %></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 %>++ <p class="text-muted mt-1">+ <%= Spree.t('admin.checkout_settings.guest_checkout.description') %>+ </p>+ </div>++ <div class="custom-control custom-checkbox mb-3">+ <%= f.check_box :preferred_company_field_enabled, class: "custom-control-input" %>+ <%= f.label :preferred_company_field_enabled, Spree.t('admin.store_form.company_field.label'), class: "custom-control-label" %>+ <%= f.error_message_on :preferred_company_field_enabled %>++ <div class="text-muted mt-1">+ <%= Spree.t('admin.store_form.company_field.description') %>+ </div>+ </div>++ <div class="custom-control custom-checkbox">+ <%= f.check_box :preferred_special_instructions_enabled, class: "custom-control-input" %>+ <%= f.label :preferred_special_instructions_enabled, Spree.t('admin.checkout_settings.special_instructions.label'), class: "custom-control-label" %>+ <%= f.error_message_on :preferred_special_instructions_enabled %>++ <p class="text-muted mt-1">+ <%= Spree.t('admin.checkout_settings.special_instructions.description') %>+ </p></div></div></div>diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.ymlindex 99b60cf241..be578e7805 100644--- a/admin/config/locales/en.yml+++ b/admin/config/locales/en.yml@@ -43,6 +43,9 @@ en:description: Allow customers to checkout without creating an account.label: Allow guest checkoutpolicy_copy: Checkout footer links are added automatically based on the %{policies_link} content.+ special_instructions:+ description: Allow customers to add special instructions to their order.+ label: Display the special instructions fieldcopied: Copied!copy_payment_link: Copy Payment Linkcopy_to_clipboard: Copy to clipboard@@ -193,6 +196,9 @@ en:store_credit:amount_remaining_of_total: "<strong>%{amount}</strong> remaining (<strong>%{total}</strong> total)"store_form:+ company_field:+ description: It shows the company field on the address form in the My Account section and on the checkout address step.+ label: Display the company address fieldcustomer_support_email_help: This email is visible to your Store visitors in the Footer sectionnew_order_notifications_email_help: If you want to receive an email notification every time someone places an Order please provide an email address for that notification to be sent tostore_setup_tasks:diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rbindex 965a5b0712..f35e158938 100644--- a/core/app/models/spree/address.rb+++ b/core/app/models/spree/address.rb@@ -189,7 +189,7 @@ module Spreeenddef show_company_address_field?- false+ Spree::Store.current.prefers_company_field_enabled?enddef editable?diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rbindex 6d9baf6ee2..330f6a12f6 100644--- a/core/app/models/spree/stock_location.rb+++ b/core/app/models/spree/stock_location.rb@@ -29,7 +29,6 @@ module Spreeafter_update :conditional_touch_recordsdelegate :name, :iso3, :iso, :iso_name, to: :country, prefix: true- delegate :show_company_address_field?, to: :addressdef state_textstate.try(:abbr) || state.try(:name) || state_name@@ -136,6 +135,7 @@ module SpreeSpree::Address.new(address1: address1,address2: address2,+ company: company,city: city,state: state,state_name: state_name,@@ -159,6 +159,10 @@ module Spreefalseend+ def show_company_address_field?+ true+ end+def display_name@display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ')enddiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 8d6c94404c..c355b2c445 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -48,6 +48,9 @@ module Spreepreference :password_protected, :boolean, default: false# Checkout preferencespreference :guest_checkout, :boolean, default: true+ preference :special_instructions_enabled, :boolean, default: false+ # Address preferences+ preference :company_field_enabled, :boolean, default: false# digital assets preferencespreference :limit_digital_download_count, :boolean, default: truepreference :limit_digital_download_days, :boolean, default: truediff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 3641d5e037..e83bd361e8 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -631,6 +631,7 @@ en:successfully_updated: Updated successfullyunsuccessfully_saved: There was an error while trying to save your address.unsuccessfully_updated: There was an update while trying to update your address.+ address_settings: Address settingsaddresses: Addressesadjustable: Adjustableadjustment: Adjustmentdiff --git a/core/db/migrate/20250527134027_add_company_to_spree_stock_locations.rb b/core/db/migrate/20250527134027_add_company_to_spree_stock_locations.rbnew file mode 100644index 0000000000..c61af5facf--- /dev/null+++ b/core/db/migrate/20250527134027_add_company_to_spree_stock_locations.rb@@ -0,0 +1,5 @@+class AddCompanyToSpreeStockLocations < ActiveRecord::Migration[7.2]+ def change+ add_column :spree_stock_locations, :company, :string, if_not_exists: true+ end+enddiff --git a/core/lib/spree/core/configuration.rb b/core/lib/spree/core/configuration.rbindex 5b2d0fe615..7a9d67f81f 100644--- a/core/lib/spree/core/configuration.rb+++ b/core/lib/spree/core/configuration.rb@@ -37,7 +37,7 @@ module Spreepreference :auto_capture_on_dispatch, :boolean, default: false # Captures payment for each shipment in Shipment#after_ship callback, and makes Shipment.ready when payment authorized.preference :binary_inventory_cache, :boolean, default: false, deprecated: true # only invalidate product cache when a stock item changes whether it is in_stockpreference :checkout_zone, :string, default: nil, deprecated: true # replace with the name of a zone if you would like to limit the countries- preference :company, :boolean, default: false # Request company field for billing and shipping addr+ preference :company, :boolean, default: false, deprecated: 'Use the company_field_enabled preference in the Spree::Store model' # Request company field for billing and shipping addrpreference :currency, :string, default: 'USD', deprecated: truepreference :credit_to_new_allocation, :boolean, default: falsepreference :disable_sku_validation, :boolean, default: false # when turned off disables the built-in SKU uniqueness validationdiff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex d35b278ab6..e4ab7c6fb3 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -213,7 +213,7 @@ module Spree@@stock_item_attributes = [:variant_id, :stock_location_id, :backorderable, :count_on_hand]@@stock_location_attributes = [- :name, :active, :address1, :address2, :city, :zipcode,+ :name, :active, :address1, :address2, :city, :zipcode, :company,:backorderable_default, :state_name, :state_id, :country_id, :phone,:propagate_all_variants]diff --git a/storefront/app/views/spree/checkout/_address.html.erb b/storefront/app/views/spree/checkout/_address.html.erbindex 8e3c933b11..f84a71b8e7 100644--- a/storefront/app/views/spree/checkout/_address.html.erb+++ b/storefront/app/views/spree/checkout/_address.html.erb@@ -61,6 +61,9 @@<%= form.hidden_field :state_lock_version %><%= form.hidden_field :ship_address_id, data: { 'checkout-address-book-target': 'addressId' } %></div>++ <%= render 'spree/checkout/special_instructions', form: form %>+<div><%= button_tag Spree.t(:save_and_continue),data: {@@ -140,6 +143,8 @@</div></div>+ <%= render 'spree/checkout/special_instructions', form: form %>+<%= hidden_field_tag 'save_user_address', true, data: { hook: "save_user_address" } %></div><div class="flex justify-end w-full">diff --git a/storefront/app/views/spree/checkout/_special_instructions.html.erb b/storefront/app/views/spree/checkout/_special_instructions.html.erbnew file mode 100644index 0000000000..351be8502d--- /dev/null+++ b/storefront/app/views/spree/checkout/_special_instructions.html.erb@@ -0,0 +1,10 @@+<% if current_store.prefers_special_instructions_enabled? %>+ <div class="mt-8 mb-4">+ <h5 class="font-body mb-3">+ <%= I18n.t('activerecord.attributes.spree/order.special_instructions') %>+ </h5>+ <div class="form-group">+ <%= form.text_area :special_instructions, class: 'w-full text-input', rows: 2, placeholder: Spree.t('storefront.checkout.special_instructions_placeholder') %>+ </div>+ </div>+<% end %>diff --git a/storefront/app/views/themes/default/spree/shared/_order_details.html.erb b/storefront/app/views/themes/default/spree/shared/_order_details.html.erbindex a2af9d8ae0..eae0525667 100644--- a/storefront/app/views/themes/default/spree/shared/_order_details.html.erb+++ b/storefront/app/views/themes/default/spree/shared/_order_details.html.erb@@ -24,6 +24,17 @@<%= render partial: 'spree/shared/order_shipment', collection: shipments, as: :shipment, cached: spree_base_cache_scope %></ul>+<% if order.special_instructions.present? %>+ <div class="p-4 lg:p-6 text-sm border">+ <div class="uppercase tracking-widest mb-1">+ <%= I18n.t('activerecord.attributes.spree/order.special_instructions') %>+ </div>+ <div class="!leading-[1.375rem] text-neutral-800">+ <%= simple_format(order.special_instructions) %>+ </div>+ </div>+<% end %>+<div class="mt-6 bg-accent mb-24"><div class="grid grid-cols-1 lg:grid-cols-2 gap-4 lg:gap-6 p-4 lg:p-6 text-sm bg-border bg-accent-100"><!-- billing address -->diff --git a/storefront/config/locales/en.yml b/storefront/config/locales/en.ymlindex 170f061ca6..63f97dc0c5 100644--- a/storefront/config/locales/en.yml+++ b/storefront/config/locales/en.yml@@ -37,6 +37,7 @@ en:payment_processing_message: You will receive an email confirmation when your payment is processed.shipping_not_available: Shipping not availablesignup_for_an_account: Create an account for faster future purchases, order tracking, and more.+ special_instructions_placeholder: Here you can add any special instructions for your orderstore_credits_amount_applied: You applied a %{amount} store credit to your order.thanks_for_your_order: Thanks %{name} for your order!you_will_need_to_pay_only: You will need to pay only %{amount} with your payment method of choosing.diff --git a/storefront/lib/generators/spree/storefront/install/templates/application.tailwind.css b/storefront/lib/generators/spree/storefront/install/templates/application.tailwind.cssindex ea5f91679c..203af7d7b1 100644--- a/storefront/lib/generators/spree/storefront/install/templates/application.tailwind.css+++ b/storefront/lib/generators/spree/storefront/install/templates/application.tailwind.css@@ -78,7 +78,7 @@ html {}textarea.text-input {- height: auto;+ height: auto !important;min-height: 3rem;border-radius: 0;}diff --git a/storefront/spec/controllers/spree/addresses_controller_spec.rb b/storefront/spec/controllers/spree/addresses_controller_spec.rbindex c74b2f8812..2b788fca02 100644--- a/storefront/spec/controllers/spree/addresses_controller_spec.rb+++ b/storefront/spec/controllers/spree/addresses_controller_spec.rb@@ -52,6 +52,48 @@ describe Spree::AddressesController, type: :controller doexpect(flash[:notice]).to eq Spree.t('address_book.successfully_created')end+ describe 'company field' do+ let(:company_name) { 'User Company Inc.' }+ let(:address_params) do+ address = build(:address, company: company_name, country: country, state: state)+ address.attributes.except('created_at', 'updated_at', 'quick_checkout')+ end++ before { store.update!(preferred_company_field_enabled: true) }+ after { store.update!(preferred_company_field_enabled: false) }++ context 'when company field is provided' do+ it 'saves company field when creating address' do+ post_create++ expect(response.status).to eq(302)+ expect(user.addresses.last.company).to eq('User Company Inc.')+ end+ end++ context 'when company field is empty' do+ let(:company_name) { '' }++ it 'saves address without company field' do+ post_create++ expect(response.status).to eq(302)+ expect(user.addresses.last.company).to be_blank+ end+ end++ context 'when store has company field disabled' do+ before { store.update!(preferred_company_field_enabled: false) }++ it 'still saves company field if provided in params (backend compatibility)' do+ post_create++ expect(response.status).to eq(302)+ expect(user.addresses.last.company).to eq('User Company Inc.')+ end+ end+ end+context 'default address' dolet!(:default_address) { create(:address, user: user) }@@ -154,6 +196,45 @@ describe Spree::AddressesController, type: :controller doendend+ describe 'company field handling' do+ let(:company_name) { 'Updated Company LLC' }+ let(:address_params) { address.attributes.symbolize_keys.merge(company: company_name) }++ before { store.update!(preferred_company_field_enabled: true) }+ after { store.update!(preferred_company_field_enabled: false) }++ context 'when company field is provided' do+ it 'updates company field' do+ put_update++ expect(response.status).to eq(302)+ expect(address.reload.company).to eq('Updated Company LLC')+ end+ end++ context 'when company field is empty' do+ let(:company_name) { '' }++ it 'updates address with blank company field' do+ put_update++ expect(response.status).to eq(302)+ expect(address.reload.company).to be_blank+ end+ end++ context 'when store has company field disabled' do+ before { store.update!(preferred_company_field_enabled: false) }++ it 'still updates company field if provided in params (backend compatibility)' do+ put_update++ expect(response.status).to eq(302)+ expect(address.reload.company).to eq('Updated Company LLC')+ end+ end+ end+context 'when data is not valid' dolet(:address_params) { address.attributes.symbolize_keys.merge(firstname: nil) }diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rbindex d61a697c38..326d5564e7 100644--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb@@ -70,6 +70,32 @@ describe Spree::CheckoutController, type: :controller doexpect(assigns(:order).signup_for_an_account).to be(true)end+ describe 'special instructions field visibility' do+ before do+ order.update_column(:state, 'address')+ end++ context 'when special instructions preference is enabled' do+ before { store.update!(preferred_special_instructions_enabled: true) }+ after { store.update!(preferred_special_instructions_enabled: false) }++ it 'renders the special instructions field' do+ get :edit, params: { token: order.token, state: 'address' }+ expect(response.body).to include('special_instructions')+ expect(response.body).to include(I18n.t('activerecord.attributes.spree/order.special_instructions'))+ end+ end++ context 'when special instructions preference is disabled' do+ before { store.update!(preferred_special_instructions_enabled: false) }++ it 'does not render the special instructions field' do+ get :edit, params: { token: order.token, state: 'address' }+ expect(response.body).not_to include('special_instructions')+ end+ end+ end+context 'when user is not signed in' dolet(:user) { nil }@@ -514,6 +540,69 @@ describe Spree::CheckoutController, type: :controller doendendend++ describe 'address company field' do+ let(:company_name) { 'Test Company Inc.' }+ let(:ship_address_params) { build(:address, company: company_name, country: country, state: state).attributes.except(:user_id, :created_at, :updated_at) }++ before { store.update!(preferred_company_field_enabled: true) }+ after { store.update!(preferred_company_field_enabled: false) }++ it 'saves company field when provided' do+ update+ expect(response).to have_http_status(:redirect)+ expect(order.ship_address.company).to eq('Test Company Inc.')+ expect(order.ship_address.user).to eq(user)+ end++ context 'when company field is empty' do+ let(:company_name) { '' }++ it 'saves address without company field' do+ update++ expect(response).to have_http_status(:redirect)+ expect(order.ship_address.company).to be_blank+ expect(order.ship_address.user).to eq(user)+ end+ end+ end++ describe 'special instructions' do+ let(:ship_address) { create(:address, user: user, country: country, state: state) }+ let(:special_instructions) { "Please leave at the front door.\nKnock on the door." }+ let(:ship_address_params) { order.ship_address.attributes.except(:user_id, :created_at, :updated_at) }+ let(:update_params) do+ {+ token: order.token,+ state: 'address',+ order: {+ ship_address_attributes: ship_address_params,+ special_instructions: special_instructions+ }+ }+ end++ before { store.update!(preferred_special_instructions_enabled: true) }+ after { store.update!(preferred_special_instructions_enabled: false) }++ it 'saves special instructions when provided' do+ update+ expect(response).to have_http_status(:redirect)+ expect(order.special_instructions).to eq("Please leave at the front door.\nKnock on the door.")+ end++ context 'when special instructions is empty' do+ let(:special_instructions) { '' }++ it 'saves order without special instructions' do+ update++ expect(response).to have_http_status(:redirect)+ expect(order.special_instructions).to be_blank+ end+ end+ endendcontext 'with the order in the delivery state' do@@ -910,6 +999,36 @@ describe Spree::CheckoutController, type: :controller doexpect(user.reload.bill_address).to eq(order.billing_address)end++ describe 'billing address company field' do+ let(:bill_address_with_company) { bill_address_attributes.merge(company: company_name) }++ before { store.update!(preferred_company_field_enabled: true) }+ after { store.update!(preferred_company_field_enabled: false) }++ context 'when company field is provided' do+ let(:company_name) { 'Billing Company Corp.' }++ it 'saves company field in billing address' do+ put :update, params: { token: order.token, state: 'payment', order: { bill_address_attributes: bill_address_with_company } }++ expect(response).to have_http_status(:redirect)+ expect(order.reload.bill_address.company).to eq('Billing Company Corp.')+ expect(order.bill_address.user).to eq(user)+ end+ end++ context 'when company field is empty' do+ let(:company_name) { '' }++ it 'saves billing address without company field' do+ put :update, params: { token: order.token, state: 'payment', order: { bill_address_attributes: bill_address_with_company } }++ expect(response).to have_http_status(:redirect)+ expect(order.reload.bill_address.company).to be_blank+ end+ end+ endendendend