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
Default to the cheapest delivery rate that ships
Damian Legawiec3c76787c270d
diff --git a/spree/core/app/models/spree/stock/estimator.rb b/spree/core/app/models/spree/stock/estimator.rbindex dce970aac7..658d010bad 100644--- a/spree/core/app/models/spree/stock/estimator.rb+++ b/spree/core/app/models/spree/stock/estimator.rb@@ -28,10 +28,16 @@ module Spreeprivate+ # Shipping is what "delivery" means unless someone chooses otherwise, so+ # the cheapest *shipped* rate wins by default. Picking the cheapest of+ # all rates handed every order to free store pickup the moment a pickup+ # method existed. Profiles offering nothing shipped (pickup-only,+ # digital) still get their cheapest rate.def choose_default_delivery_rate(delivery_rates)- unless delivery_rates.empty?- delivery_rates.min_by(&:cost).selected = true- end+ return if delivery_rates.empty?++ shipped = delivery_rates.select { |rate| rate.delivery_method&.requires_address? }+ (shipped.presence || delivery_rates).min_by(&:cost).selected = trueenddef sort_delivery_rates(delivery_rates)diff --git a/spree/core/spec/models/spree/stock/estimator_spec.rb b/spree/core/spec/models/spree/stock/estimator_spec.rbindex 77ba7eecc6..a2af15a812 100644--- a/spree/core/spec/models/spree/stock/estimator_spec.rb+++ b/spree/core/spec/models/spree/stock/estimator_spec.rb@@ -11,6 +11,42 @@ module Spreelet(:order) { build(:order_with_line_items, ship_address: ship_address) }let(:inventory_units) { order.inventory_units }+ # A free pickup option must not silently become every order's delivery+ # method — "delivery" means shipping unless the buyer says otherwise.+ describe 'default rate selection' do+ # Pickup only quotes from a counter customers may collect at, and the+ # package's location must be the persisted one the method serves.+ let(:counter) { create(:stock_location, pickup_enabled: true) }+ let(:package) do+ build(:stock_package, stock_location: counter,+ contents: inventory_units.map { |_i| ContentItem.new(inventory_unit) })+ end+ let(:pickup) { create(:pickup_delivery_method, name: 'Store Pickup') }++ before do+ pickup.calculator.update!(preferences: { amount: 0, currency: 'USD' })+ shipping_method.calculator.update!(preferences: { amount: 5, currency: 'USD' })+ end++ it 'selects the cheapest rate that ships, not a cheaper pickup rate' do+ allow(package).to receive_messages(eligible_delivery_methods: [pickup, shipping_method])++ rates = subject.delivery_rates(package)++ # Pickup is free and therefore sorts first, but shipping is default.+ expect(rates.first.delivery_method).to eq(pickup)+ expect(rates.detect(&:selected).delivery_method).to eq(shipping_method)+ end++ it 'falls back to the cheapest rate when nothing ships' do+ allow(package).to receive_messages(eligible_delivery_methods: [pickup])++ rates = subject.delivery_rates(package)++ expect(rates.detect(&:selected).delivery_method).to eq(pickup)+ end+ end+context '#shipping rates' dobefore doallow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(true)