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
Fixed storefront so it works with both new prefixed IDs and old URLs
Damian Legawiec75b632a66057
diff --git a/core/app/finders/spree/orders/find_complete.rb b/core/app/finders/spree/orders/find_complete.rbindex 6ada10d139..efba8063cb 100644--- a/core/app/finders/spree/orders/find_complete.rb+++ b/core/app/finders/spree/orders/find_complete.rb@@ -3,11 +3,13 @@ module Spreeclass FindCompleteinclude Spree::Orders::FinderHelper- attr_reader :user, :number, :token, :store, :email+ attr_reader :user, :number, :prefix_id, :param, :token, :store, :email- def initialize(user: nil, number: nil, token: nil, store: nil, email: nil)+ def initialize(user: nil, number: nil, prefix_id: nil, param: nil, token: nil, store: nil, email: nil)@user = user@number = number+ @prefix_id = prefix_id+ @param = param@token = token@store = store@email = email@@ -16,6 +18,8 @@ module Spreedef executeorders = by_user(scope)orders = by_number(orders)+ orders = by_prefix_id(orders)+ orders = by_param(orders)orders = by_token(orders)orders = by_store(orders)orders = by_email(orders)@@ -37,6 +41,14 @@ module Spreenumber.present?end+ def prefix_id?+ prefix_id.present?+ end++ def param?+ param.present?+ end+def token?token.present?end@@ -61,6 +73,20 @@ module Spreeorders.where(number: number)end+ def by_prefix_id(orders)+ return orders unless prefix_id?++ orders.where(prefix_id: prefix_id)+ end++ # Find by param - tries prefix_id first, then number for backwards compatibility+ def by_param(orders)+ return orders unless param?++ # Try prefix_id first, then number+ orders.where(prefix_id: param).or(orders.where(number: param))+ end+def by_token(orders)return orders unless token?diff --git a/core/app/models/concerns/spree/prefixed_id.rb b/core/app/models/concerns/spree/prefixed_id.rbindex f1177108c7..b3aa4b2367 100644--- a/core/app/models/concerns/spree/prefixed_id.rb+++ b/core/app/models/concerns/spree/prefixed_id.rb@@ -57,6 +57,29 @@ module Spreedef prefix_id_prefix_prefix_id_prefixend++ # Find a record by prefix_id first, falling back to id for backwards compatibility+ # @param param [String] the prefix_id or id to search for+ # @return [ActiveRecord::Base, nil] the found record or nil+ def find_by_param(param)+ return nil if param.blank?++ # Try prefix_id first (new format)+ record = find_by(prefix_id: param)+ return record if record++ # Fall back to id (legacy format) - only if param looks like an integer+ find_by(id: param) if param.to_s.match?(/\A\d+\z/)+ end++ # Find a record by prefix_id first, falling back to id for backwards compatibility+ # Raises ActiveRecord::RecordNotFound if not found+ # @param param [String] the prefix_id or id to search for+ # @return [ActiveRecord::Base] the found record+ # @raise [ActiveRecord::RecordNotFound] if record not found+ def find_by_param!(param)+ find_by_param(param) || raise(ActiveRecord::RecordNotFound.new("Couldn't find #{name} with param=#{param}"))+ endenddef generate_prefix_iddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 6a7c5687dd..3fd08cd40f 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -236,6 +236,33 @@ module Spreeleft_joins(:bill_address).where(arel_table[:email].lower.eq(query.downcase)).or(where(conditions.reduce(:or)))end+ # Find an order by prefix_id first, falling back to number, then id for backwards compatibility+ # @param param [String] the prefix_id, number, or id to search for+ # @return [Spree::Order, nil] the found order or nil+ def self.find_by_param(param)+ return nil if param.blank?++ # Try prefix_id first (new format)+ order = find_by(prefix_id: param)+ return order if order++ # Try number (legacy format)+ order = find_by(number: param)+ return order if order++ # Fall back to id (numeric legacy format) - only if param looks like an integer+ find_by(id: param) if param.to_s.match?(/\A\d+\z/)+ end++ # Find an order by prefix_id first, falling back to number, then id for backwards compatibility+ # Raises ActiveRecord::RecordNotFound if not found+ # @param param [String] the prefix_id, number, or id to search for+ # @return [Spree::Order] the found order+ # @raise [ActiveRecord::RecordNotFound] if order not found+ def self.find_by_param!(param)+ find_by_param(param) || raise(ActiveRecord::RecordNotFound.new("Couldn't find Order with param=#{param}"))+ end+# Use this method in other gems that wish to register their own custom logic# that should be called after Order#updatedef self.register_update_hook(hook)diff --git a/storefront/app/controllers/spree/account/orders_controller.rb b/storefront/app/controllers/spree/account/orders_controller.rbindex b91b37decd..8a365a71e9 100644--- a/storefront/app/controllers/spree/account/orders_controller.rb+++ b/storefront/app/controllers/spree/account/orders_controller.rb@@ -30,7 +30,7 @@ module Spreeenddef load_order_details- @order = orders_scope.find_by!(number: params[:id])+ @order = order_finder.new(user: try_spree_current_user, store: current_store, param: params[:id]).execute.first!@shipments = @order.shipments.includes(:stock_location, :address, selected_shipping_rate: :shipping_method, inventory_units: :line_item)endenddiff --git a/storefront/app/controllers/spree/addresses_controller.rb b/storefront/app/controllers/spree/addresses_controller.rbindex 3f6362031b..d95f71104a 100644--- a/storefront/app/controllers/spree/addresses_controller.rb+++ b/storefront/app/controllers/spree/addresses_controller.rb@@ -105,7 +105,7 @@ module Spree@address ||= if [:new, :create].include?(action)Spree::Address.new(country: current_store.default_country, user: try_spree_current_user)else- Spree::Address.find(params[:id])+ Spree::Address.find_by_param!(params[:id])endauthorize! action, @addressdiff --git a/storefront/app/controllers/spree/line_items_controller.rb b/storefront/app/controllers/spree/line_items_controller.rbindex 992518fa1f..c2d4d7ab66 100644--- a/storefront/app/controllers/spree/line_items_controller.rb+++ b/storefront/app/controllers/spree/line_items_controller.rb@@ -81,7 +81,7 @@ module Spreeenddef load_line_item- @line_item = @order.line_items.find(params[:id])+ @line_item = @order.line_items.find_by_param!(params[:id])enddef line_item_paramsdiff --git a/storefront/app/controllers/spree/orders_controller.rb b/storefront/app/controllers/spree/orders_controller.rbindex e4db18a709..1d18dc3f44 100644--- a/storefront/app/controllers/spree/orders_controller.rb+++ b/storefront/app/controllers/spree/orders_controller.rb@@ -16,7 +16,7 @@ module Spree# GET /orders/:iddef show- @order = complete_order_finder.new(number: params[:id], token: params[:token], store: current_store).execute.first+ @order = complete_order_finder.new(param: params[:id], token: params[:token], store: current_store).execute.firstraise ActiveRecord::RecordNotFound if @order.blank?diff --git a/storefront/spec/features/checkout_steps_spec.rb b/storefront/spec/features/checkout_steps_spec.rbindex 1a558970f1..30cd74f63c 100644--- a/storefront/spec/features/checkout_steps_spec.rb+++ b/storefront/spec/features/checkout_steps_spec.rb@@ -228,12 +228,13 @@ describe 'Checkout steps (address and delivery)' doclick_on 'Save'click_on 'Save and Continue'expect(page).to have_content(user.email)- expect(page).to have_content(user.shipping_address.firstname)- expect(page).to have_content(user.shipping_address.lastname)- expect(page).to have_content(user.shipping_address.address1)- expect(page).to have_content(user.shipping_address.address2)- expect(page).to have_content(user.shipping_address.city)- expect(page).to have_content(user.shipping_address.zipcode)+ # After adding a new address, verify the NEW address is shown on delivery step+ expect(page).to have_content('Guest')+ expect(page).to have_content('Guestovski')+ expect(page).to have_content('Bul ASNOM')+ expect(page).to have_content('77')+ expect(page).to have_content('Skopje')+ expect(page).to have_content('77777')expect(page).to have_content(Spree::ShippingMethod.first.name)endend