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.

Use global pricing context per request to speed up Pricing (#13446)

Damian Legawiec3a49fd76162a
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index b787ca0f11..b95cbd5fe1 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -87,6 +87,9 @@ RSpec.configure do |config|
# Request specs to paths with ?locale=xx don't reset the locale afterwards
# Some tests assume that the current locale is :en, so we ensure it here
I18n.locale = :en
+
+ # Reset Spree::Current to avoid stale memoized values between tests
+ Spree::Current.reset
end
config.around(:each) do |example|
diff --git a/core/app/models/spree/current.rb b/core/app/models/spree/current.rb
index bd7572dfbf..c5f2d14bb6 100644
--- a/core/app/models/spree/current.rb
+++ b/core/app/models/spree/current.rb
@@ -1,6 +1,6 @@
module Spree
class Current < ::ActiveSupport::CurrentAttributes
- attribute :store, :currency, :zone
+ attribute :store, :currency, :zone, :price_lists, :global_pricing_context
def store
super || Spree::Store.default
@@ -13,5 +13,22 @@ module Spree
def zone
super || Spree::Zone.default_tax || store&.checkout_zone
end
+
+ def price_lists
+ super || begin
+ context = global_pricing_context
+ self.price_lists = Spree::PriceList.for_context(context)
+ end
+ end
+
+ def global_pricing_context
+ super || begin
+ self.global_pricing_context = Spree::Pricing::Context.new(
+ currency: currency,
+ store: store,
+ zone: zone
+ )
+ end
+ end
end
end
diff --git a/core/lib/spree/core/pricing/context.rb b/core/lib/spree/core/pricing/context.rb
index 3da39ce79f..bf43160df8 100644
--- a/core/lib/spree/core/pricing/context.rb
+++ b/core/lib/spree/core/pricing/context.rb
@@ -12,7 +12,7 @@ module Spree
# @param quantity [Integer]
# @param date [Time]
# @param order [Spree::Order]
- def initialize(variant:, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)
+ def initialize(variant: nil, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)
@variant = variant
@currency = currency
@store = store || Spree::Current.store
diff --git a/core/lib/spree/core/pricing/resolver.rb b/core/lib/spree/core/pricing/resolver.rb
index e1497f1019..2469f4fdb6 100644
--- a/core/lib/spree/core/pricing/resolver.rb
+++ b/core/lib/spree/core/pricing/resolver.rb
@@ -44,12 +44,17 @@ module Spree
# Returns the price lists that are applicable to the context
# @return [Array<Spree::PriceList>]
def applicable_price_lists
- @applicable_price_lists ||= begin
- lists = Spree::PriceList.includes(:price_rules)
- .for_context(context)
- .to_a
+ @applicable_price_lists ||= price_lists_for_context.select { |list| list.applicable?(context) }
+ end
- lists.select { |list| list.applicable?(context) }
+ # Returns the price lists for the context's store
+ # Uses Spree::Current.price_lists if the context matches, otherwise fetches directly
+ # @return [ActiveRecord::Relation<Spree::PriceList>]
+ def price_lists_for_context
+ if context.store == Spree::Current.store && context.currency == Spree::Current.currency
+ Spree::Current.price_lists
+ else
+ Spree::PriceList.for_context(context)
end
end
diff --git a/core/spec/models/spree/current_spec.rb b/core/spec/models/spree/current_spec.rb
index ba620f5735..4a8822b4df 100644
--- a/core/spec/models/spree/current_spec.rb
+++ b/core/spec/models/spree/current_spec.rb
@@ -82,6 +82,79 @@ RSpec.describe Spree::Current do
end
end
+ describe '#global_pricing_context' do
+ let!(:store) { create(:store, default: true, default_currency: 'USD') }
+ let(:zone) { create(:zone, default_tax: true) }
+
+ before do
+ described_class.store = store
+ described_class.currency = 'USD'
+ described_class.zone = zone
+ end
+
+ it 'returns a Spree::Pricing::Context' do
+ expect(described_class.global_pricing_context).to be_a(Spree::Pricing::Context)
+ end
+
+ it 'uses the current store' do
+ expect(described_class.global_pricing_context.store).to eq(store)
+ end
+
+ it 'uses the current currency' do
+ expect(described_class.global_pricing_context.currency).to eq('USD')
+ end
+
+ it 'uses the current zone' do
+ expect(described_class.global_pricing_context.zone).to eq(zone)
+ end
+
+ it 'memoizes the context' do
+ context1 = described_class.global_pricing_context
+ context2 = described_class.global_pricing_context
+ expect(context1).to be(context2)
+ end
+ end
+
+ describe '#price_lists' do
+ let!(:store) { create(:store, default: true) }
+ let!(:other_store) { create(:store) }
+ let!(:active_price_list) { create(:price_list, :active, store: store, position: 1) }
+ let!(:scheduled_price_list) { create(:price_list, :scheduled, store: store, position: 2, starts_at: 1.day.ago, ends_at: 1.day.from_now) }
+ let!(:inactive_price_list) { create(:price_list, :inactive, store: store) }
+ let!(:other_store_price_list) { create(:price_list, :active, store: other_store) }
+
+ before do
+ described_class.store = store
+ end
+
+ it 'returns price lists for the current store' do
+ expect(described_class.price_lists).to include(active_price_list)
+ expect(described_class.price_lists).not_to include(other_store_price_list)
+ end
+
+ it 'includes active price lists' do
+ expect(described_class.price_lists).to include(active_price_list)
+ end
+
+ it 'includes scheduled price lists within date range' do
+ expect(described_class.price_lists).to include(scheduled_price_list)
+ end
+
+ it 'excludes inactive price lists' do
+ expect(described_class.price_lists).not_to include(inactive_price_list)
+ end
+
+ it 'returns price lists ordered by position' do
+ expect(described_class.price_lists.to_a).to eq([active_price_list, scheduled_price_list])
+ end
+
+ it 'memoizes the price lists' do
+ lists1 = described_class.price_lists
+ lists2 = described_class.price_lists
+ expect(lists1).to be(lists2)
+ end
+ end
+
describe '.reset' do
let(:store) { create(:store) }
let(:zone) { create(:zone) }
@@ -105,5 +178,25 @@ RSpec.describe Spree::Current do
# Zone falls back to default tax zone
expect(described_class.zone).not_to eq(zone)
end
+
+ it 'clears memoized price_lists' do
+ # Access price_lists to memoize
+ described_class.price_lists
+
+ described_class.reset
+
+ # After reset, price_lists should be fetched fresh
+ expect(described_class.instance_variable_get(:@price_lists)).to be_nil
+ end
+
+ it 'clears memoized global_pricing_context' do
+ # Access global_pricing_context to memoize
+ described_class.global_pricing_context
+
+ described_class.reset
+
+ # After reset, global_pricing_context should be fetched fresh
+ expect(described_class.instance_variable_get(:@global_pricing_context)).to be_nil
+ end
end
end