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
Make order routing strategy resolution graceful with fallback (#14140)
Damian Legawiec2d525262c6fc
diff --git a/docs/developer/how-to/custom-order-routing.mdx b/docs/developer/how-to/custom-order-routing.mdxindex 80074da6de..04b4e36552 100644--- a/docs/developer/how-to/custom-order-routing.mdx+++ b/docs/developer/how-to/custom-order-routing.mdx@@ -243,7 +243,7 @@ store.channels.find_by(code: 'pos').update!()```-Resolution order: `channel.preferred_order_routing_strategy` → `store.preferred_order_routing_strategy`. The class is `safe_constantize`-d and rejected if it doesn't subclass `Strategy::Base`.+Resolution order: `channel.preferred_order_routing_strategy` → `store.preferred_order_routing_strategy` → the default `Strategy::Rules`. Only a value that points to a real `Strategy::Base` subclass is used; anything unset or invalid is skipped, so a misconfigured strategy falls back to the default instead of breaking checkout.### Step 3: Test the Strategydiff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex 1a89c0ad73..20eed1b1fc 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -228,7 +228,6 @@"inherit": "Inherit from store","options": {"Spree::OrderRouting::Strategy::Rules": "Rules (ordered)",- "Spree::OrderRouting::Strategy::Reducer": "Reducer (greedy)","Spree::OrderRouting::Strategy::Legacy": "Legacy"}}diff --git a/packages/dashboard/src/schemas/channel.ts b/packages/dashboard/src/schemas/channel.tsindex d2c8ea438e..c101e28b85 100644--- a/packages/dashboard/src/schemas/channel.ts+++ b/packages/dashboard/src/schemas/channel.ts@@ -6,7 +6,6 @@ import { z } from 'zod/v4'export const ORDER_ROUTING_STRATEGY_VALUES = ['','Spree::OrderRouting::Strategy::Rules',- 'Spree::OrderRouting::Strategy::Reducer','Spree::OrderRouting::Strategy::Legacy',] as constdiff --git a/spree/admin/app/helpers/spree/admin/channels_helper.rb b/spree/admin/app/helpers/spree/admin/channels_helper.rbindex fcd0efb3e5..96ebc6db2e 100644--- a/spree/admin/app/helpers/spree/admin/channels_helper.rb+++ b/spree/admin/app/helpers/spree/admin/channels_helper.rb@@ -7,7 +7,6 @@ module Spreedef channel_order_routing_strategy_options[[Spree.t('admin.channels.order_routing_strategies.rules'), 'Spree::OrderRouting::Strategy::Rules'],- [Spree.t('admin.channels.order_routing_strategies.reducer'), 'Spree::OrderRouting::Strategy::Reducer'],[Spree.t('admin.channels.order_routing_strategies.legacy'), 'Spree::OrderRouting::Strategy::Legacy']]enddiff --git a/spree/admin/config/locales/en.yml b/spree/admin/config/locales/en.ymlindex 8f420e79ca..8e0df23529 100644--- a/spree/admin/config/locales/en.yml+++ b/spree/admin/config/locales/en.yml@@ -81,7 +81,6 @@ en:default_hint: Exactly one channel per store is the default. Used as the fallback when no channel is selected and as the auto-publish target for new products.order_routing_strategies:legacy: Legacy- reducer: Reducer (greedy)rules: Rules (ordered)order_routing_strategy: Order routing strategyorder_routing_strategy_hint: Overrides the store-level routing strategy for orders attributed to this channel.diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 6a5f2d664e..72370f02f0 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -776,15 +776,18 @@ module Spreeendend+ # Resolves the routing strategy from the channel override first, then the+ # store default. Any preference that doesn't resolve to a concrete+ # Spree::OrderRouting::Strategy::Base subclass (e.g. an internal collaborator+ # like Reducer that was never a strategy, or a typo'd custom class) is logged+ # and skipped rather than raised, falling back to the default Rules strategy+ # so a misconfiguration can't take down cart display or checkout.+ ## @return [Spree::OrderRouting::Strategy::Base]def order_routing_strategy- klass_name = channel&.preferred_order_routing_strategy.presence ||- store.preferred_order_routing_strategy- klass = klass_name&.safe_constantize-- unless klass && klass <= Spree::OrderRouting::Strategy::Base- raise ArgumentError, "Invalid order routing strategy: #{klass_name.inspect}"- end+ klass = valid_order_routing_strategy_class(channel&.preferred_order_routing_strategy) ||+ valid_order_routing_strategy_class(store.preferred_order_routing_strategy) ||+ Spree::OrderRouting::Strategy::Rulesklass.new(order: self)end@@ -1041,6 +1044,19 @@ module Spreeprivate+ def valid_order_routing_strategy_class(klass_name)+ return if klass_name.blank?++ klass = klass_name.safe_constantize+ return klass if klass && klass < Spree::OrderRouting::Strategy::Base++ Rails.logger.warn(+ "[Spree] Ignoring invalid order routing strategy #{klass_name.inspect} " \+ "for order #{number.inspect}; falling back to the default strategy."+ )+ nil+ end+def link_by_emailself.email = user.email if userenddiff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rbindex 638e688ef6..4bfd754cb4 100644--- a/spree/core/spec/models/spree/order_spec.rb+++ b/spree/core/spec/models/spree/order_spec.rb@@ -1612,6 +1612,26 @@ describe Spree::Order, type: :model doexpect(channel.preferred_order_routing_strategy).to be_blankexpect(isolated_order.order_routing_strategy).to be_a(Spree::OrderRouting::Strategy::Rules)end++ it 'skips an invalid channel preference and uses the store preference' do+ stub_const('StorePreferredStrategy', Class.new(Spree::OrderRouting::Strategy::Base))+ isolated_store = create(:store)+ isolated_store.update!(preferred_order_routing_strategy: 'StorePreferredStrategy')+ channel = isolated_store.channels.first+ # Reducer is an internal collaborator of Rules, not a Strategy::Base.+ channel.update!(preferred_order_routing_strategy: 'Spree::OrderRouting::Strategy::Reducer')+ isolated_order = create(:order, store: isolated_store, channel: channel)++ expect(isolated_order.order_routing_strategy).to be_a(StorePreferredStrategy)+ end++ it 'falls back to the default Rules strategy when the persisted value is invalid' do+ isolated_store = create(:store)+ isolated_store.update!(preferred_order_routing_strategy: 'Spree::OrderRouting::Strategy::Reducer')+ isolated_order = create(:order, store: isolated_store)++ expect(isolated_order.order_routing_strategy).to be_a(Spree::OrderRouting::Strategy::Rules)+ endenddescribe '#ensure_channel_presence' do