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
Extend `ShippingMethod` with `delivery_range` and `display_estimated_price`
Damian Legawieceb7b960997f1
diff --git a/core/app/models/spree/shipping_method.rb b/core/app/models/spree/shipping_method.rbindex eb33686639..85fb7bf7cf 100644--- a/core/app/models/spree/shipping_method.rb+++ b/core/app/models/spree/shipping_method.rb@@ -11,14 +11,14 @@ module Spreeinclude Spree::VendorConcernend+ extend Spree::DisplayMoney+# Used for #refresh_ratesDISPLAY_ON_FRONT_END = 1DISPLAY_ON_BACK_END = 2default_scope { where(deleted_at: nil) }- scope :with_calculator, ->(calculator) { joins(:calculator).where(calculator: { type: calculator.to_s }) }-has_many :shipping_method_categories, dependent: :destroyhas_many :shipping_categories, through: :shipping_method_categorieshas_many :shipping_rates, inverse_of: :shipping_method@@ -31,9 +31,18 @@ module Spreebelongs_to :tax_category, -> { with_deleted }, class_name: 'Spree::TaxCategory', optional: truevalidates :name, :display_on, presence: true-+ validates :estimated_transit_business_days_min, numericality: { greater_than_or_equal_to: 1 }, allow_nil: true+ validates :estimated_transit_business_days_max, numericality: { greater_than_or_equal_to: 1 }, allow_nil: truevalidate :at_least_one_shipping_category+ if defined?(PgSearch)+ # full text search+ include PgSearch::Model+ pg_search_scope :search_by_name, against: %i[name]+ else+ scope :search_by_name, ->(query) { where('name LIKE ?', "%#{query}%") }+ end+def include?(address)return false unless address@@ -74,6 +83,38 @@ module Spree(backend? && display_filter == DISPLAY_ON_BACK_END)end+ def delivery_range+ return unless estimated_transit_business_days_min || estimated_transit_business_days_max++ if estimated_transit_business_days_min == estimated_transit_business_days_max+ estimated_transit_business_days_min.to_s+ else+ "#{estimated_transit_business_days_min}-#{estimated_transit_business_days_max}"+ end+ end++ def display_estimated_price+ return unless calculator++ @display_estimated_price ||= begin+ calculator.description + ': ' +++ if calculator.is_a?(Spree::Calculator::Shipping::FlatRate)+ if calculator.preferred_amount == 0+ Spree.t(:free)+ else+ Spree::Money.new(calculator.preferred_amount, { currency: calculator.preferred_currency }).to_s+ end+ elsif calculator.is_a?(Spree::Calculator::Shipping::FlexiRate)+ Spree::Money.new(calculator.preferred_first_item, { currency: calculator.preferred_currency }).to_s+ elsif calculator.is_a?(Spree::Calculator::Shipping::FlatPercentItemTotal)+ ActionController::Base.helpers.number_to_percentage(calculator.preferred_flat_percent, precision: 2)+ else+ ''+ end+ end+ end+private# Some shipping methods are only meant to be set via backenddiff --git a/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rb b/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rbnew file mode 100644index 0000000000..c0a25658c9--- /dev/null+++ b/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rb@@ -0,0 +1,6 @@+class AddEstimatedTransitFieldsToSpreeShippingMethods < ActiveRecord::Migration[7.2]+ def change+ add_column :spree_shipping_methods, :estimated_transit_business_days_min, :integer+ add_column :spree_shipping_methods, :estimated_transit_business_days_max, :integer+ end+enddiff --git a/core/spec/models/spree/shipping_method_spec.rb b/core/spec/models/spree/shipping_method_spec.rbindex 66b083e48e..1b4b832400 100644--- a/core/spec/models/spree/shipping_method_spec.rb+++ b/core/spec/models/spree/shipping_method_spec.rb@@ -166,4 +166,32 @@ describe Spree::ShippingMethod, type: :model doit { expect(backend_shipping_method.send(:backend?)).to be true }it { expect(front_and_back_end_shipping_method.send(:backend?)).to be true }end++ describe '#delivery_range' do+ context 'without set estimated_transit_business_days_min and estimated_transit_business_days_max' do+ it { expect(shipping_method.delivery_range).to be_nil }+ end++ context 'with set estimated_transit_business_days_min and estimated_transit_business_days_max' do+ let(:shipping_method) { build(:shipping_method, estimated_transit_business_days_min: 1, estimated_transit_business_days_max: 2) }++ it { expect(shipping_method.delivery_range).to eq('1-2') }+ end++ context 'when both are the same' do+ let(:shipping_method) { build(:shipping_method, estimated_transit_business_days_min: 1, estimated_transit_business_days_max: 1) }++ it { expect(shipping_method.delivery_range).to eq('1') }+ end+ end++ describe '#display_estimated_price' do+ it { expect(shipping_method.display_estimated_price).to eq('Flat rate: Free') }++ context 'with calculator' do+ let(:shipping_method) { build(:shipping_method, calculator: create(:shipping_calculator)) }++ it { expect(shipping_method.display_estimated_price).to eq('Flat rate: $10.00') }+ end+ endend