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.

Added the freight rate provider, unpriced rates and volume tiers

Damian Legawiec6e50ccf66fe8
diff --git a/spree/core/app/models/spree/delivery_method_rules/company_rule.rb b/spree/core/app/models/spree/delivery_method_rules/company_rule.rb
new file mode 100644
index 0000000000..87480bc795
--- /dev/null
+++ b/spree/core/app/models/spree/delivery_method_rules/company_rule.rb
@@ -0,0 +1,24 @@
+module Spree
+ module DeliveryMethodRules
+ # Splits freight from parcel by who is buying.
+ #
+ # On, the method is offered only to orders placed for a company — a
+ # carton or container tier disappears from retail carts. Off, it is
+ # offered only to orders that are not, which is how a merchant keeps
+ # parcel methods away from wholesale buyers. A method with no company
+ # rule at all is offered to both, so the rule is only ever added to
+ # state a split.
+ #
+ # Presence only. Which company gets which freight terms is Enterprise
+ # territory, and open-source Spree has no company roles to gate on.
+ class CompanyRule < Spree::DeliveryMethodRule
+ preference :company_orders_only, :boolean, default: true
+
+ def eligible?(package)
+ buying_for_a_company = package.owner&.b2b? || false
+
+ preferred_company_orders_only ? buying_for_a_company : !buying_for_a_company
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/delivery_method_rules/volume_rule.rb b/spree/core/app/models/spree/delivery_method_rules/volume_rule.rb
new file mode 100644
index 0000000000..a30c6f09c0
--- /dev/null
+++ b/spree/core/app/models/spree/delivery_method_rules/volume_rule.rb
@@ -0,0 +1,35 @@
+module Spree
+ module DeliveryMethodRules
+ # Bounds the method by the packed volume of the shipment, in cubic
+ # meters. This is what turns a wholesale tier table into configuration: a
+ # "Pallet" method takes 1–15 CBM, a "20ft container" 15–33, and the
+ # estimator offers whichever one the order actually fills.
+ #
+ # Unlike {WeightRule}, the compared value is always converted to CBM
+ # first. Dimensions are stored in whatever unit the merchant typed, and
+ # inches read as centimeters overstate a carton roughly sixteenfold — too
+ # large an error to leave to convention.
+ class VolumeRule < Spree::DeliveryMethodRule
+ preference :minimum_volume, :decimal, default: nil, nullable: true
+ preference :maximum_volume, :decimal, default: nil, nullable: true
+
+ def eligible?(package)
+ summary = package.freight_summary
+ volume = summary.total_volume
+
+ # A partly measured catalog understates the load — wholly unmeasured
+ # goods roll up to zero, and one measured SKU among five reports a
+ # fraction of the truth. Either way the figure is a floor, not a
+ # measurement, so it may raise a shipment into a tier but never
+ # exclude it from one: failing a minimum on an understated volume
+ # hides the method from exactly the order it was built for, with
+ # nothing for the merchant to see. A maximum still applies, since
+ # passing one on too small a number errs toward offering the method.
+ return false if preferred_minimum_volume.present? && summary.complete? && volume < preferred_minimum_volume
+ return false if preferred_maximum_volume.present? && volume > preferred_maximum_volume
+
+ true
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/delivery_rate.rb b/spree/core/app/models/spree/delivery_rate.rb
index 8294169bd7..aabbe3cb31 100644
--- a/spree/core/app/models/spree/delivery_rate.rb
+++ b/spree/core/app/models/spree/delivery_rate.rb
@@ -34,6 +34,8 @@ module Spree
end
def display_price
+ return quoted_after_review if unpriced?
+
price = display_base_price.to_s
return price if tax_rate.nil? || tax_amount.zero? || !tax_rate.show_rate_in_label
@@ -49,11 +51,25 @@ module Spree
alias display_cost display_price
alias_attribute :base_price, :cost
- # Returns true if the shipping rate is free
+ # Returns true if the shipping rate is free.
+ #
+ # An unpriced rate is never free — its cost is zero because nobody has
+ # quoted it yet, and saying "free shipping" over a container of goods
+ # would be a promise the merchant cannot keep.
#
# @return [Boolean]
def free?
- final_price.zero?
+ !unpriced? && final_price.zero?
+ end
+
+ # The logistics the freight provider computed when it quoted this rate —
+ # cartons, pallets, cubic meters, gross weight. Frozen at estimate time
+ # and never re-derived, so an order keeps reporting what it actually
+ # shipped as.
+ #
+ # @return [Spree::FreightSummary, nil]
+ def freight_summary
+ @freight_summary ||= Spree::FreightSummary.from_metadata(metadata&.dig('freight_summary'))
end
# Returns the tax amount for the shipping rate, computed directly from
@@ -98,6 +114,22 @@ module Spree
end
end
alias total final_price
+
+ # Every money display on an unpriced rate says the same thing. A
+ # storefront that renders the total rather than the cost would otherwise
+ # put "$0.00" — free freight — in front of a buyer whose shipment has not
+ # been quoted yet.
+ #
+ # Keeps the generated method's shape: the same keyword options, and a
+ # Spree::Money on every priced path, so callers doing money arithmetic on
+ # the total are unaffected.
+ #
+ # @return [String, Spree::Money]
+ def display_final_price(**options)
+ return quoted_after_review if unpriced?
+
+ Spree::Money.new(final_price, { currency: currency }.merge(options))
+ end
alias display_total display_final_price
# Returns the delivery range for the shipping method
@@ -120,6 +152,11 @@ module Spree
private
+ # What every money display on this rate reads instead of an amount.
+ def quoted_after_review
+ Spree.t('delivery_rates.quoted_after_review')
+ end
+
def discount_amount
fulfillment.discounts.promotion.sum(:amount)
end
diff --git a/spree/core/app/models/spree/delivery_rate_provider/estimate.rb b/spree/core/app/models/spree/delivery_rate_provider/estimate.rb
index 0f8b16e2ed..6cc83570b8 100644
--- a/spree/core/app/models/spree/delivery_rate_provider/estimate.rb
+++ b/spree/core/app/models/spree/delivery_rate_provider/estimate.rb
@@ -11,6 +11,11 @@ module Spree
include ActiveModel::Attributes
attribute :cost, :decimal
+ # True when the price is not known yet and will be quoted separately —
+ # freight a forwarder rates after the merchant reviews the order. The
+ # estimator skips markup and tax for these, and display surfaces say so
+ # rather than showing the zero cost as free shipping.
+ attribute :unpriced, :boolean, default: false
# ISO code of the currency the provider quoted in. Nil means the
# store's own currency (Internal); carrier quotes come back in the
# carrier account's currency and are dropped when it differs from the
diff --git a/spree/core/app/models/spree/delivery_rate_provider/freight.rb b/spree/core/app/models/spree/delivery_rate_provider/freight.rb
new file mode 100644
index 0000000000..c3c1e589c8
--- /dev/null
+++ b/spree/core/app/models/spree/delivery_rate_provider/freight.rb
@@ -0,0 +1,41 @@
+module Spree
+ module DeliveryRateProvider
+ # Quotes nothing, and says so.
+ #
+ # International freight is priced by a forwarder after someone looks at
+ # the order — the carton count, the cubic meters, the destination port —
+ # not by an API at checkout. So this provider returns a rate with no
+ # price, carrying the logistics summary the merchant will send to the
+ # forwarder in place of an amount.
+ #
+ # Which freight method a shipment is offered is
+ # {Spree::DeliveryMethodRules::VolumeRule}'s job: a merchant configures
+ # "Pallet" and "40ft container" as ordinary delivery methods on this
+ # provider, each bounded by the cubic meters it covers.
+ #
+ # See docs/plans/6.0-b2b-wholesale-shipping.md.
+ class Freight < Base
+ # There is no price to calculate, so the method's calculator is never
+ # consulted and admin UIs hide its pricing form.
+ def self.uses_calculator?
+ false
+ end
+
+ # Freight ships to an address like any carrier does.
+ def self.requires_address?
+ true
+ end
+
+ # @param package [Spree::Stock::Package]
+ # @return [Spree::DeliveryRateProvider::Estimate]
+ def estimate(package)
+ Estimate.new(
+ cost: 0,
+ unpriced: true,
+ name: delivery_method.name,
+ metadata: { 'freight_summary' => package.freight_summary.as_json }
+ )
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rb
index e54a62e2df..14a43feab6 100644
--- a/spree/core/app/models/spree/fulfillment.rb
+++ b/spree/core/app/models/spree/fulfillment.rb
@@ -25,7 +25,12 @@ module Spree
with_options dependent: :delete_all do
has_many :fulfillment_items, class_name: 'Spree::FulfillmentItem', inverse_of: :fulfillment
- has_many :delivery_rates, -> { order(:cost) }, class_name: 'Spree::DeliveryRate'
+ # Cheapest first, with unpriced rates after every priced one — a rate
+ # quoted after review costs zero only because nobody has priced it, so
+ # ordering on cost alone would list freight above real quotes. Matches
+ # the order the Estimator hands back, so a rate read from the database
+ # sorts the same way as one just computed.
+ has_many :delivery_rates, -> { order(:unpriced, :cost) }, class_name: 'Spree::DeliveryRate'
end
has_many :tax_lines, class_name: 'Spree::TaxLine', dependent: :destroy, inverse_of: :fulfillment
has_many :discounts, class_name: 'Spree::Discount', dependent: :destroy, inverse_of: :fulfillment
@@ -113,7 +118,14 @@ module Spree
super
end
end
- delegate :amount_in_cents, to: :display_cost
+
+ # Read off the raw cost rather than the formatted one: an unpriced freight
+ # fulfillment formats as "quoted after review", and a String has no cents.
+ #
+ # @return [Integer]
+ def amount_in_cents
+ Spree::Money.new(cost, currency: currency).amount_in_cents
+ end
# Statuses describe what the merchant did with the package — nothing else.
# Payment and stock readiness used to be encoded here as pending/ready and
@@ -193,6 +205,26 @@ module Spree
extend DisplayMoney
money_methods :cost, :discounted_cost, :final_price, :item_cost, :additional_tax_total, :included_tax_total, :tax_total, :discount_total
+
+ # True while the shipping price is still the forwarder's to quote — the
+ # selected rate is a freight rate carrying logistics instead of an amount.
+ #
+ # @return [Boolean]
+ def unpriced?
+ selected_delivery_rate&.unpriced? || false
+ end
+
+ # An unpriced fulfillment's cost is zero only because nobody has priced
+ # it, and every display reading that zero as money — the order
+ # fulfillments card included — would call a container of goods free
+ # shipping. Same doctrine as the rate's own displays.
+ #
+ # @return [String, Spree::Money]
+ def display_cost(**options)
+ return Spree.t('delivery_rates.quoted_after_review') if unpriced?
+
+ Spree::Money.new(cost, { currency: currency }.merge(options))
+ end
alias display_amount display_cost
# Standardized column name (renamed in 6.0); the legacy reader stays one
# release.
@@ -300,13 +332,26 @@ module Spree
cost + adjustment_total
end
alias total final_price
+ # Every money display on an unpriced fulfillment says the same thing, for
+ # the same reason display_cost does: a container nobody has quoted must
+ # not render as free.
+ #
+ # @return [String, Spree::Money]
+ def display_final_price(**options)
+ return Spree.t('delivery_rates.quoted_after_review') if unpriced?
+
+ Spree::Money.new(final_price, { currency: currency }.merge(options))
+ end
alias display_total display_final_price
def final_price_with_items
item_cost + final_price
end
+ # An unpriced fulfillment is never free — its cost is zero only because
+ # the forwarder has not quoted it yet.
def free?
+ return false if unpriced?
return true if final_price == BigDecimal(0)
with_free_shipping_promotion?
diff --git a/spree/core/app/models/spree/stock/estimator.rb b/spree/core/app/models/spree/stock/estimator.rb
index 842ca81bb2..0204d82750 100644
--- a/spree/core/app/models/spree/stock/estimator.rb
+++ b/spree/core/app/models/spree/stock/estimator.rb
@@ -51,11 +51,18 @@ module Spree
return if delivery_rates.empty?
shipped = delivery_rates.select { |rate| rate.delivery_method&.requires_address? }
- (shipped.presence || delivery_rates).min_by(&:cost).selected = true
+ candidates = shipped.presence || delivery_rates
+ # An unpriced rate costs zero, so preselecting on price alone would
+ # hand every mixed offering to freight. Prefer something actually
+ # priced; fall back to freight when that is all there is.
+ priced = candidates.reject(&:unpriced?)
+ (priced.presence || candidates).min_by(&:cost).selected = true
end
+ # Cheapest first, with unpriced rates after the priced ones — their zero
+ # cost is an absence of information, not a bargain.
def sort_delivery_rates(delivery_rates)
- delivery_rates.sort_by!(&:cost)
+ delivery_rates.sort_by! { |rate| [rate.unpriced? ? 1 : 0, rate.cost] }
end
# Quoting runs through the method's rate provider — Internal prices
@@ -78,11 +85,11 @@ module Spree
end
service_row = delivery_method.service_for(estimate)
- cost = apply_markup(estimate.cost, delivery_method, service_row, provider)
delivery_method.delivery_rates.new(
- cost: gross_amount(cost, taxation_options_for(delivery_method)),
- tax_rate: first_tax_rate_for(delivery_method.tax_category),
+ cost: rate_cost(estimate, delivery_method, service_row, provider),
+ tax_rate: (first_tax_rate_for(delivery_method.tax_category) unless estimate.unpriced),
+ unpriced: estimate.unpriced,
name: rate_name(estimate, service_row),
carrier: estimate.carrier,
service_level: estimate.service_level,
@@ -95,6 +102,17 @@ module Spree
end
end
+ # An unpriced quote has no amount to mark up or tax — a percentage of an
+ # unknown price is still unknown, and rounding zero through the VAT
+ # gross-up would only invent a number. Its cost stays zero and every
+ # display surface reads +unpriced+ instead.
+ def rate_cost(estimate, delivery_method, service_row, provider)
+ return 0 if estimate.unpriced
+
+ cost = apply_markup(estimate.cost, delivery_method, service_row, provider)
+ gross_amount(cost, taxation_options_for(delivery_method))
+ end
+
# Handling fee on top of provider quotes: the service row's values when
# set, else the method-level defaults. Calculator-priced methods are
# exempt — their calculator IS the price.
@@ -168,15 +186,12 @@ module Spree
package_seller_id = package.stock_location&.seller_id
methods.select do |delivery_method|
- calculator = delivery_method.calculator
-
offered_to_seller?(delivery_method, package_seller_id) &&
delivery_method.available_to?(audience) &&
delivery_method.include?(order.ship_address) &&
delivery_method.serves_location?(package.stock_location) &&
delivery_method.eligible_for_package?(package) &&
- calculator.available?(package) &&
- calculator.supports_currency?(currency)
+ calculator_offers?(delivery_method, package)
end
end
@@ -190,6 +205,27 @@ module Spree
delivery_method.seller_id.nil? && delivery_method.available_to_sellers?
end
+ # The calculator's say in eligibility.
+ #
+ # `available?` is a documented extension point — a host app overrides it
+ # to refuse a method for a package — so it applies to every method,
+ # however the method is priced.
+ #
+ # The currency check does not. A provider-priced method carries a
+ # calculator the admin hides and nothing reads an amount from, so its
+ # leftover currency would silence exactly the methods built for foreign
+ # trade: a freight method's default calculator speaks only the default
+ # store's currency, and international wholesale is what the freight
+ # provider exists for. Provider quotes get their currency check where it
+ # belongs, against the estimate itself.
+ def calculator_offers?(delivery_method, package)
+ calculator = delivery_method.calculator
+ return false unless calculator.available?(package)
+ return true unless delivery_method.rate_provider_instance.class.uses_calculator?
+
+ calculator.supports_currency?(currency)
+ end
+
# True when a subclass or decorator redefined the legacy seam, so its
# filtering still applies.
def overrides_legacy_seam?
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 4ceb24e695..2cd8d94479 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -363,6 +363,12 @@ en:
not_allowed_content_type: has not allowed content type
external_video_url:
unsupported_video_provider: must be a YouTube or Vimeo link
+ spree/package_type:
+ attributes:
+ default:
+ cannot_be_given_up: cannot be turned off on the only default package — make another one the default instead
+ kind:
+ cannot_stop_being_a_carton: cannot change while products are packed into this carton — repack them first
spree/payment:
attributes:
amount:
@@ -446,6 +452,8 @@ en:
attributes:
base:
cannot_destroy_if_attached_to_line_items: Cannot delete Variants that are added to placed Orders. In such cases, please discontinue them.
+ carton_package_type:
+ must_be_a_carton: must be a package type of kind carton
order_multiple:
incompatible_with_carton: must divide into or be a multiple of %{units_per_carton} units per carton
units_per_carton:
@@ -939,15 +947,23 @@ en:
channel_rule:
description: Offers the delivery method only on the selected channels
name: Channel
+ company_rule:
+ description: Offers the delivery method only to orders placed for a company, or only to orders that are not
+ name: Company
excluded_products_rule:
description: Blocks the delivery method for packages containing the selected products
name: Excluded products
item_total_rule:
description: Limits the delivery method to carts within an item total range
name: Item total
+ volume_rule:
+ description: Limits the delivery method to shipments within a volume range, in cubic meters
+ name: Volume
weight_rule:
description: Limits the delivery method to packages within a weight range
name: Weight
+ delivery_rates:
+ quoted_after_review: Quoted after review
depth: Depth
description: Description
destination: Destination
@@ -1015,6 +1031,7 @@ en:
already_a_member: already exists
cannot_delete_channel_with_active_api_keys: Channel cannot be deleted while active API keys are bound to it. Revoke those keys first.
cannot_delete_default_channel: Default channel cannot be deleted. Promote another channel to default first.
+ cannot_delete_default_package_type: Default package cannot be deleted. Make another package the default first.
cart_currency_mismatch: carts in different currencies cannot be merged
channel_store_mismatch: must belong to the same store
commission_rate_needs_an_amount: needs a flat fee amount in at least one currency
@@ -1387,6 +1404,8 @@ en:
other: Other
out_of_stock: Out of Stock
outstanding_balance: Outstanding Balance
+ package_types:
+ default_name: Shipping box
page: Page
pages: Pages
password: Password
diff --git a/spree/core/db/migrate/20260831100003_add_unpriced_to_spree_delivery_rates.rb b/spree/core/db/migrate/20260831100003_add_unpriced_to_spree_delivery_rates.rb
new file mode 100644
index 0000000000..97581cc5e6
--- /dev/null
+++ b/spree/core/db/migrate/20260831100003_add_unpriced_to_spree_delivery_rates.rb
@@ -0,0 +1,8 @@
+class AddUnpricedToSpreeDeliveryRates < ActiveRecord::Migration[8.1]
+ def change
+ # A rate whose price is not known yet — freight quoted by a forwarder
+ # after the merchant reviews the order. Distinct from a rate that costs
+ # nothing, which is what a bare zero would say.
+ add_column :spree_delivery_rates, :unpriced, :boolean, null: false, default: false
+ end
+end
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index 3b24068fb8..d42b38ffa2 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -867,6 +867,7 @@ require 'spree/core/engine'
require 'spree/i18n'
require 'spree/iso_data'
require 'spree/localized_number'
+require 'spree/measurement'
require 'spree/translations'
require 'spree/money'
require 'spree/service_module'
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 8d54cd35a4..f152a50236 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -328,7 +328,8 @@ module Spree
# Quoting strategies selectable on a delivery method. Internal prices
# through the method's calculator; carrier gems append theirs.
Rails.application.config.spree.delivery_rate_providers.concat [
- Spree::DeliveryRateProvider::Internal
+ Spree::DeliveryRateProvider::Internal,
+ Spree::DeliveryRateProvider::Freight
]
# Digital asset sources. Core ships the uploaded-file default; host
@@ -390,7 +391,9 @@ module Spree
Spree::DeliveryMethodRules::ItemTotalRule,
Spree::DeliveryMethodRules::WeightRule,
Spree::DeliveryMethodRules::ExcludedProductsRule,
- Spree::DeliveryMethodRules::ChannelRule
+ Spree::DeliveryMethodRules::ChannelRule,
+ Spree::DeliveryMethodRules::VolumeRule,
+ Spree::DeliveryMethodRules::CompanyRule
]
Rails.application.config.spree.calculators.promotion_actions_create_adjustments = [
diff --git a/spree/core/spec/models/spree/delivery_method_rule_spec.rb b/spree/core/spec/models/spree/delivery_method_rule_spec.rb
index 39721afb15..74b2b16fc4 100644
--- a/spree/core/spec/models/spree/delivery_method_rule_spec.rb
+++ b/spree/core/spec/models/spree/delivery_method_rule_spec.rb
@@ -106,6 +106,102 @@ describe Spree::DeliveryMethodRule, type: :model do
end
end
+ describe Spree::DeliveryMethodRules::VolumeRule do
+ let(:carton) { create(:carton_package_type, store: store, length: 100, width: 100, height: 100) }
+ # Two cartons at one cubic meter each.
+ let(:freight_package) do
+ variant = create(:variant, units_per_carton: 6, carton_package_type: carton)
+ order = create(:order, store: store)
+ create(:line_item, order: order, variant: variant, quantity: 12)
+ Spree::Stock::Coordinator.new(order.reload).packages.first
+ end
+
+ # The tiers a merchant configures are ranges over this one number, so the
+ # comparison has to be converted volume, never the raw dimension columns:
+ # two one-meter cartons are 2 CBM, not 2,000,000.
+ # A catalog nobody measured rolls up to zero cubic meters, which is not
+ # the same as a small shipment — failing a minimum on it would hide the
+ # method with nothing for the merchant to diagnose.
+ it 'offers the method when the goods were never measured' do
+ unmeasured = create(:variant, width: nil, height: nil, depth: nil, weight: 0)
+ order = create(:order, store: store)
+ create(:line_item, order: order, variant: unmeasured, quantity: 1)
+ package = Spree::Stock::Coordinator.new(order.reload).packages.first
+
+ rule = described_class.new(delivery_method: delivery_method, preferred_minimum_volume: 1)
+
+ expect(rule.eligible?(package)).to be(true)
+ end
+
+ # One measured SKU among unmeasured ones reports a fraction of the real
+ # load; failing a minimum on that hides the method from the wholesale
+ # order it exists for.
+ it 'offers the method when the measurement is only partial' do
+ measured = create(:variant, units_per_carton: 6, carton_package_type: carton)
+ unmeasured = create(:variant, width: nil, height: nil, depth: nil, weight: 0)
+ order = create(:order, store: store)
+ create(:line_item, order: order, variant: measured, quantity: 6)
+ create(:line_item, order: order, variant: unmeasured, quantity: 40)
+ package = Spree::Stock::Coordinator.new(order.reload).packages.first
+
+ rule = described_class.new(delivery_method: delivery_method, preferred_minimum_volume: 5)
+
+ expect(package.freight_summary).not_to be_complete
+ expect(rule.eligible?(package)).to be(true)
+ end
+
+ it 'bounds by the packed volume in cubic meters' do
+ rule = described_class.new(delivery_method: delivery_method)
+ expect(rule.eligible?(freight_package)).to be(true)
+
+ rule.preferred_minimum_volume = 2
+ expect(rule.eligible?(freight_package)).to be(true)
+
+ rule.preferred_minimum_volume = 3
+ expect(rule.eligible?(freight_package)).to be(false)
+
+ rule.preferred_minimum_volume = nil
+ rule.preferred_maximum_volume = 1
+ expect(rule.eligible?(freight_package)).to be(false)
+ end
+ end
+
+ describe Spree::DeliveryMethodRules::CompanyRule do
+ let(:customer) { create(:user) }
+ let(:company) { create(:company, store: store) }
+
+ let(:company_package) do
+ create(:company_membership, company: company, customer: customer)
+ cart = create(:cart, store: store, user: customer, company: company)
+ create(:line_item, cart: cart, order: nil)
+ fulfillment = create(:shipment, cart: cart, order: nil, stock_location: create(:stock_location))
+ fulfillment.to_package
+ end
+
+ # Adding the rule at all is what states a split, so it defaults to the
+ # side that needs stating: freight is for company orders.
+ it 'is for company orders by default' do
+ rule = described_class.new(delivery_method: delivery_method)
+
+ expect(rule.eligible?(company_package)).to be(true)
+ expect(rule.eligible?(package)).to be(false)
+ end
+
+ it 'hides a freight method from a retail cart' do
+ rule = described_class.new(delivery_method: delivery_method, preferred_company_orders_only: true)
+
+ expect(rule.eligible?(company_package)).to be(true)
+ expect(rule.eligible?(package)).to be(false)
+ end
+
+ it 'hides a parcel method from a company cart when turned off' do
+ rule = described_class.new(delivery_method: delivery_method, preferred_company_orders_only: false)
+
+ expect(rule.eligible?(company_package)).to be(false)
+ expect(rule.eligible?(package)).to be(true)
+ end
+ end
+
describe Spree::DeliveryMethodRules::ExcludedProductsRule do
it 'blocks packages containing an excluded product and fails open without products' do
rule = described_class.create!(delivery_method: delivery_method)
diff --git a/spree/core/spec/models/spree/delivery_rate_provider/freight_spec.rb b/spree/core/spec/models/spree/delivery_rate_provider/freight_spec.rb
new file mode 100644
index 0000000000..ca7d5d02f5
--- /dev/null
+++ b/spree/core/spec/models/spree/delivery_rate_provider/freight_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+RSpec.describe Spree::DeliveryRateProvider::Freight do
+ let(:store) { @default_store }
+ let(:carton) { create(:carton_package_type, store: store, length: 40, width: 30, height: 25) }
+ let(:variant) { create(:variant, units_per_carton: 12, cartons_per_pallet: 40, carton_package_type: carton) }
+ let(:delivery_method) do
+ create(:delivery_method, store: store, name: 'Pallet freight',
+ rate_provider: described_class.name)
+ end
+ let(:package) do
+ order = create(:order, store: store)
+ create(:line_item, order: order, variant: variant, quantity: 24)
+ Spree::Stock::Coordinator.new(order.reload).packages.first
+ end
+
+ subject(:provider) { described_class.new(delivery_method) }
+
+ it 'quotes a rate with no price rather than a free one' do
+ estimate = provider.estimate(package)
+
+ expect(estimate.unpriced).to be(true)
+ expect(estimate.cost).to be_zero
+ end
+
+ it 'names the rate after the method the merchant configured' do
+ expect(provider.estimate(package).name).to eq('Pallet freight')
+ end
+
+ # What the merchant sends the forwarder in place of a price.
+ it 'carries the freight summary the forwarder will quote against' do
+ summary = provider.estimate(package).metadata['freight_summary']
+
+ expect(summary['total_units']).to eq(24)
+ expect(summary['total_cartons']).to eq(2)
+ expect(summary['total_pallets']).to eq(1)
+ expect(summary['complete']).to be(true)
+ end
+
+ it 'ships to an address and never consults a calculator' do
+ expect(described_class.requires_address?).to be(true)
+ expect(described_class.uses_calculator?).to be(false)
+ end
+
+ it 'is registered as a selectable rate provider' do
+ expect(Spree.delivery_rate_providers).to include(described_class)
+ end
+end
diff --git a/spree/core/spec/models/spree/delivery_rate_spec.rb b/spree/core/spec/models/spree/delivery_rate_spec.rb
index cad59b61a0..01464db265 100644
--- a/spree/core/spec/models/spree/delivery_rate_spec.rb
+++ b/spree/core/spec/models/spree/delivery_rate_spec.rb
@@ -303,4 +303,26 @@ describe Spree::DeliveryRate, type: :model do
end
end
end
+
+ describe 'an unpriced rate' do
+ let(:rate) { create(:delivery_rate, cost: 0, unpriced: true) }
+
+ it 'is not free — nobody has priced it yet' do
+ expect(rate).not_to be_free
+ end
+
+ it 'says so on the cost and the total alike' do
+ expect(rate.display_cost).to eq(Spree.t('delivery_rates.quoted_after_review'))
+ expect(rate.display_total).to eq(Spree.t('delivery_rates.quoted_after_review'))
+ end
+ end
+
+ describe 'a priced rate' do
+ let(:rate) { create(:delivery_rate, cost: 12) }
+
+ it 'still renders its total as money, options and all' do
+ expect(rate.display_total).to be_a(Spree::Money)
+ expect(rate.display_total(currency: 'EUR').currency.iso_code).to eq('EUR')
+ end
+ end
end
diff --git a/spree/core/spec/models/spree/stock/estimator_spec.rb b/spree/core/spec/models/spree/stock/estimator_spec.rb
index 689ee3ba73..14bccee445 100644
--- a/spree/core/spec/models/spree/stock/estimator_spec.rb
+++ b/spree/core/spec/models/spree/stock/estimator_spec.rb
@@ -427,6 +427,116 @@ module Spree
expect(rate.metadata['quote_id']).to eq('rate_123')
end
+ let(:unpriced_provider_class) do
+ Class.new(Spree::DeliveryRateProvider::Base) do
+ def estimate(_package)
+ Spree::DeliveryRateProvider::Estimate.new(cost: 0, unpriced: true, name: 'Freight')
+ end
+ end
+ end
+
+ # A provider-priced method carries a calculator nobody reads; its
+ # leftover currency must not hide the method from foreign carts —
+ # freight exists for exactly those.
+ describe 'a freight method on a foreign-currency cart' do
+ let(:eur_order) { create(:order_with_line_items, currency: 'EUR') }
+
+ before do
+ # A relation, not the outer describe's array: an order with a
+ # store reaches the store-methods merge, which arrays cannot.
+ allow(package).to receive_messages(
+ eligible_delivery_methods: Spree::DeliveryMethod.where(id: delivery_method.id)
+ )
+ end
+
+ it 'is offered although its unused calculator only speaks the store currency' do
+ delivery_method.update!(rate_provider: 'Spree::DeliveryRateProvider::Freight')
+ expect(delivery_method.calculator.supports_currency?('EUR')).to be(false)
+
+ rates = Spree::Stock::Estimator.new(eur_order).delivery_rates(package)
+
+ expect(rates.map(&:delivery_method)).to include(delivery_method)
+ expect(rates.detect { |rate| rate.delivery_method == delivery_method }.unpriced).to be(true)
+ end
+
+ it 'still hides a calculator-priced method that cannot price the currency' do
+ expect(delivery_method.calculator.supports_currency?('EUR')).to be(false)
+
+ rates = Spree::Stock::Estimator.new(eur_order).delivery_rates(package)
+
+ expect(rates.map(&:delivery_method)).not_to include(delivery_method)
+ end
+ end
+
+ describe 'unpriced rates' do
+ before do
+ stub_const('UnpricedRateProvider', unpriced_provider_class)
+ allow(delivery_method).to receive(:rate_provider_instance).
+ and_return(unpriced_provider_class.new(delivery_method))
+ end
+
+ it 'marks the rate unpriced rather than pricing it at nothing' do
+ rate = subject.delivery_rates(package).first
+
+ expect(rate.unpriced).to be(true)
+ expect(rate).not_to be_free
+ end
+
+ # A percentage of an unknown price is still unknown, so the handling
+ # markup a merchant set for real carrier quotes must not apply.
+ it 'skips the method markup' do
+ delivery_method.update!(markup_percent: 50, markup_flat: 10)
+
+ expect(subject.delivery_rates(package).first.cost).to be_zero
+ end
+
+ it 'resolves no tax rate against an amount nobody has quoted' do
+ expect(subject.delivery_rates(package).first.tax_rate).to be_nil
+ end
+
+ it 'says the price is coming rather than showing an amount' do
+ expect(subject.delivery_rates(package).first.display_price).
+ to eq(Spree.t('delivery_rates.quoted_after_review'))
+ end
+ end
+
+ # A zero cost would otherwise sort first and be preselected, handing
+ # every mixed offering to freight.
+ describe 'unpriced rates beside priced ones' do
+ let(:priced_method) { create(:delivery_method) }
+
+ before do
+ allow(priced_method.calculator).to receive(:compute).and_return(BigDecimal('25'))
+
+ stub_const('MixedFreightProvider', unpriced_provider_class)
+ allow(delivery_method).to receive(:rate_provider_instance).
+ and_return(unpriced_provider_class.new(delivery_method))
+ allow(package).to receive_messages(eligible_delivery_methods: [delivery_method, priced_method])
+ end
+
+ it 'preselects the priced rate' do
+ selected = subject.delivery_rates(package).detect(&:selected)
+
+ expect(selected.delivery_method).to eq(priced_method)
+ end
+
+ it 'sorts the unpriced rate last' do
+ expect(subject.delivery_rates(package).last.unpriced).to be(true)
+ end
+
+ # The Estimator's in-memory order is thrown away the moment the
+ # rates are persisted and read back through the association, which
+ # is what the storefront actually receives.
+ it 'keeps the unpriced rate last when the rates are read back' do
+ fulfillment = create(:shipment)
+ subject.delivery_rates(package).each do |rate|
+ rate.update!(fulfillment: fulfillment)
+ end
+
+ expect(fulfillment.reload.delivery_rates.last.unpriced).to be(true)
+ end
+ end
+
# One carrier method now yields one rate per service (decisions.md
# 2026-08-09) — the provider returns several estimates and the
# estimator fans them out, names them, and applies merchant controls.