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.

Connected Markets with Price Lists

Damian Legawiec10556367cb92
diff --git a/backend/engines/admin/app/helpers/spree/admin/base_helper.rb b/backend/engines/admin/app/helpers/spree/admin/base_helper.rb
index de73304b2e..0cda9356a3 100644
--- a/backend/engines/admin/app/helpers/spree/admin/base_helper.rb
+++ b/backend/engines/admin/app/helpers/spree/admin/base_helper.rb
@@ -361,6 +361,11 @@ module Spree
return Spree.t(:none) if zone_ids.empty?
Spree::Zone.where(id: zone_ids).pluck(:name).join(', ')
+ when :market_ids
+ market_ids = Array(value).reject(&:blank?).map(&:to_i)
+ return Spree.t(:none) if market_ids.empty?
+
+ Spree::Market.where(id: market_ids).pluck(:name).join(', ')
when :user_ids
user_ids = Array(value).reject(&:blank?).map(&:to_i)
return Spree.t(:none) if user_ids.empty?
diff --git a/backend/engines/admin/app/views/spree/admin/price_rules/forms/_market_rule.html.erb b/backend/engines/admin/app/views/spree/admin/price_rules/forms/_market_rule.html.erb
new file mode 100644
index 0000000000..659bbf4a30
--- /dev/null
+++ b/backend/engines/admin/app/views/spree/admin/price_rules/forms/_market_rule.html.erb
@@ -0,0 +1,7 @@
+<div class="form-group">
+ <%= f.label :preferred_market_ids, Spree.t(:markets) %>
+ <%= f.select :preferred_market_ids,
+ current_store.markets.order(:name).pluck(:name, :id),
+ { selected: f.object.preferred_market_ids },
+ { multiple: true, data: { controller: 'autocomplete-select' } } %>
+</div>
diff --git a/backend/engines/core/app/models/spree/current.rb b/backend/engines/core/app/models/spree/current.rb
index 999ffcba52..e9227621d0 100644
--- a/backend/engines/core/app/models/spree/current.rb
+++ b/backend/engines/core/app/models/spree/current.rb
@@ -30,7 +30,8 @@ module Spree
self.global_pricing_context = Spree::Pricing::Context.new(
currency: currency,
store: store,
- zone: zone
+ zone: zone,
+ market: market
)
end
end
diff --git a/backend/engines/core/app/models/spree/price_rules/market_rule.rb b/backend/engines/core/app/models/spree/price_rules/market_rule.rb
new file mode 100644
index 0000000000..8a849590f9
--- /dev/null
+++ b/backend/engines/core/app/models/spree/price_rules/market_rule.rb
@@ -0,0 +1,19 @@
+module Spree
+ module PriceRules
+ class MarketRule < Spree::PriceRule
+ preference :market_ids, :array, default: []
+
+ def applicable?(context)
+ return false unless context.market
+ return true if preferred_market_ids.empty?
+
+ # Compare as strings to support both integer and UUID primary keys
+ preferred_market_ids.map(&:to_s).include?(context.market.id.to_s)
+ end
+
+ def self.description
+ 'Apply pricing based on the market'
+ end
+ end
+ end
+end
diff --git a/backend/engines/core/config/locales/en.yml b/backend/engines/core/config/locales/en.yml
index b85fa21c7e..8f4bc9b7b8 100644
--- a/backend/engines/core/config/locales/en.yml
+++ b/backend/engines/core/config/locales/en.yml
@@ -1360,6 +1360,7 @@ en:
master: Master
master_price: Master Price
market: Market
+ market_ids: Markets
markets: Markets
master_sku: Master SKU
match_choices:
@@ -1774,6 +1775,9 @@ en:
zone_rule:
description: Apply pricing based on geographic zone
name: Zone
+ market_rule:
+ description: Apply pricing based on the market
+ name: Market
price_rules:
customer_group_rule:
description: Apply pricing to specific customer groups
diff --git a/backend/engines/core/lib/spree/core/engine.rb b/backend/engines/core/lib/spree/core/engine.rb
index 7f5b4b5d9a..a2bd4b2670 100644
--- a/backend/engines/core/lib/spree/core/engine.rb
+++ b/backend/engines/core/lib/spree/core/engine.rb
@@ -168,6 +168,7 @@ module Spree
Rails.application.config.spree.pricing.rules.concat [
Spree::PriceRules::ZoneRule,
+ Spree::PriceRules::MarketRule,
Spree::PriceRules::UserRule,
Spree::PriceRules::CustomerGroupRule,
Spree::PriceRules::VolumeRule
diff --git a/backend/engines/core/lib/spree/core/pricing/context.rb b/backend/engines/core/lib/spree/core/pricing/context.rb
index bf43160df8..647d88c9ba 100644
--- a/backend/engines/core/lib/spree/core/pricing/context.rb
+++ b/backend/engines/core/lib/spree/core/pricing/context.rb
@@ -1,22 +1,24 @@
module Spree
module Pricing
class Context
- attr_reader :variant, :currency, :store, :zone, :user, :quantity, :date, :order
+ attr_reader :variant, :currency, :store, :zone, :market, :user, :quantity, :date, :order
# Initializes the context
# @param variant [Spree::Variant]
# @param currency [String]
# @param store [Spree::Store]
# @param zone [Spree::Zone]
+ # @param market [Spree::Market]
# @param user [Spree::User]
# @param quantity [Integer]
# @param date [Time]
# @param order [Spree::Order]
- def initialize(variant: nil, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)
+ def initialize(variant: nil, currency:, store: nil, zone: nil, market: nil, user: nil, quantity: nil, date: nil, order: nil)
@variant = variant
@currency = currency
@store = store || Spree::Current.store
@zone = zone || Spree::Current.zone
+ @market = market || Spree::Current.market
@user = user
@quantity = quantity
@date = date || Time.current
@@ -53,6 +55,7 @@ module Spree
currency,
store&.id,
zone&.id,
+ market&.id,
user&.id,
quantity,
date&.to_i
diff --git a/backend/engines/core/lib/spree/testing_support/factories/price_rule_factory.rb b/backend/engines/core/lib/spree/testing_support/factories/price_rule_factory.rb
index 3959513d53..4e09f8ee99 100644
--- a/backend/engines/core/lib/spree/testing_support/factories/price_rule_factory.rb
+++ b/backend/engines/core/lib/spree/testing_support/factories/price_rule_factory.rb
@@ -45,5 +45,15 @@ FactoryBot.define do
customer_group_ids { [] }
end
end
+
+ factory :market_price_rule, class: Spree::PriceRules::MarketRule do
+ after(:build) do |rule, evaluator|
+ rule.preferred_market_ids = evaluator.market_ids if evaluator.respond_to?(:market_ids)
+ end
+
+ transient do
+ market_ids { [] }
+ end
+ end
end
end
diff --git a/backend/engines/core/spec/lib/spree/core/pricing/context_spec.rb b/backend/engines/core/spec/lib/spree/core/pricing/context_spec.rb
index 265f8175f1..0baf20bfaa 100644
--- a/backend/engines/core/spec/lib/spree/core/pricing/context_spec.rb
+++ b/backend/engines/core/spec/lib/spree/core/pricing/context_spec.rb
@@ -7,6 +7,7 @@ module Spree
let(:currency) { 'USD' }
let(:store) { @default_store }
let(:zone) { create(:zone) }
+ let(:market) { create(:market, store: store) }
let(:user) { create(:user) }
let(:quantity) { 5 }
let(:date) { Time.zone.parse('2024-01-15 12:00:00') }
@@ -19,6 +20,7 @@ module Spree
currency: currency,
store: store,
zone: zone,
+ market: market,
user: user,
quantity: quantity,
date: date
@@ -30,6 +32,7 @@ module Spree
expect(subject.currency).to eq(currency)
expect(subject.store).to eq(store)
expect(subject.zone).to eq(zone)
+ expect(subject.market).to eq(market)
expect(subject.user).to eq(user)
expect(subject.quantity).to eq(quantity)
expect(subject.date).to eq(date)
@@ -56,6 +59,10 @@ module Spree
expect(subject.zone).to eq(Spree::Current.zone)
end
+ it 'defaults market to Spree::Current.market' do
+ expect(subject.market).to eq(Spree::Current.market)
+ end
+
it 'defaults user to nil' do
expect(subject.user).to be_nil
end
@@ -100,6 +107,34 @@ module Spree
expect(subject.zone).to eq(current_zone)
end
end
+
+ context 'when Spree::Current.market is set' do
+ let(:current_market) { create(:market) }
+
+ before { Spree::Current.market = current_market }
+ after { Spree::Current.reset }
+
+ subject do
+ described_class.new(variant: variant, currency: currency)
+ end
+
+ it 'uses Spree::Current.market as default' do
+ expect(subject.market).to eq(current_market)
+ end
+ end
+
+ context 'when market is explicitly nil' do
+ before { Spree::Current.market = nil }
+ after { Spree::Current.reset }
+
+ subject do
+ described_class.new(variant: variant, currency: currency, market: nil)
+ end
+
+ it 'falls back to Spree::Current.market' do
+ expect(subject.market).to eq(Spree::Current.market)
+ end
+ end
end
describe '.from_currency' do
@@ -200,6 +235,7 @@ module Spree
currency: currency,
store: store,
zone: zone,
+ market: market,
user: user,
quantity: quantity,
date: specific_date
@@ -214,6 +250,7 @@ module Spree
currency,
store.id,
zone.id,
+ market.id,
user.id,
quantity,
specific_date.to_i
@@ -240,6 +277,7 @@ module Spree
Spree::Current.store.id
]
expected_parts << Spree::Current.zone.id if Spree::Current.zone
+ expected_parts << Spree::Current.market.id if Spree::Current.market
expected_parts << Time.current.to_i
expect(subject.cache_key).to eq(expected_parts.join('/'))
@@ -269,6 +307,7 @@ module Spree
Spree::Current.store.id
]
expected_parts << Spree::Current.zone.id if Spree::Current.zone
+ expected_parts << Spree::Current.market.id if Spree::Current.market
expected_parts << user.id
expected_parts << quantity
expected_parts << Time.current.to_i
diff --git a/backend/engines/core/spec/models/spree/current_spec.rb b/backend/engines/core/spec/models/spree/current_spec.rb
index 4a8822b4df..aceb1603b2 100644
--- a/backend/engines/core/spec/models/spree/current_spec.rb
+++ b/backend/engines/core/spec/models/spree/current_spec.rb
@@ -82,14 +82,45 @@ RSpec.describe Spree::Current do
end
end
+ describe '#market' do
+ context 'when market is set' do
+ let(:market) { create(:market) }
+
+ before { described_class.market = market }
+
+ it 'returns the set market' do
+ expect(described_class.market).to eq(market)
+ end
+ end
+
+ context 'when market is not set' do
+ let!(:store) { create(:store, default: true) }
+ let!(:market) { create(:market, store: store, default: true) }
+
+ it 'returns the default market from the store' do
+ expect(described_class.market).to eq(market)
+ end
+ end
+
+ context 'when market is not set and store has no markets' do
+ let!(:store) { create(:store, default: true) }
+
+ it 'returns nil' do
+ expect(described_class.market).to be_nil
+ end
+ end
+ end
+
describe '#global_pricing_context' do
let!(:store) { create(:store, default: true, default_currency: 'USD') }
let(:zone) { create(:zone, default_tax: true) }
+ let(:market) { create(:market, store: store) }
before do
described_class.store = store
described_class.currency = 'USD'
described_class.zone = zone
+ described_class.market = market
end
it 'returns a Spree::Pricing::Context' do
@@ -108,6 +139,10 @@ RSpec.describe Spree::Current do
expect(described_class.global_pricing_context.zone).to eq(zone)
end
+ it 'uses the current market' do
+ expect(described_class.global_pricing_context.market).to eq(market)
+ end
+
it 'memoizes the context' do
context1 = described_class.global_pricing_context
context2 = described_class.global_pricing_context
diff --git a/backend/engines/core/spec/models/spree/price_rules/market_rule_spec.rb b/backend/engines/core/spec/models/spree/price_rules/market_rule_spec.rb
new file mode 100644
index 0000000000..aeb06ae4ba
--- /dev/null
+++ b/backend/engines/core/spec/models/spree/price_rules/market_rule_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe Spree::PriceRules::MarketRule, type: :model do
+ let(:price_list) { create(:price_list) }
+ let(:rule) { create(:market_price_rule, price_list: price_list) }
+ let(:market) { create(:market) }
+ let(:variant) { create(:variant) }
+
+ describe '#applicable?' do
+ context 'when market_ids preference is empty' do
+ before { rule.preferred_market_ids = [] }
+
+ it 'returns true for any market' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: market)
+ expect(rule.applicable?(context)).to be true
+ end
+ end
+
+ context 'when market_ids preference is set' do
+ before { rule.preferred_market_ids = [market.id] }
+
+ it 'returns true when context market matches' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: market)
+ expect(rule.applicable?(context)).to be true
+ end
+
+ it 'returns false when context market does not match' do
+ other_market = create(:market)
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: other_market)
+ expect(rule.applicable?(context)).to be false
+ end
+
+ it 'returns false when context has no market' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: nil)
+ expect(rule.applicable?(context)).to be false
+ end
+ end
+
+ context 'when market_ids preference contains strings' do
+ before { rule.preferred_market_ids = [market.id.to_s] }
+
+ it 'returns true when context market matches' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: market)
+ expect(rule.applicable?(context)).to be true
+ end
+
+ it 'returns false when context market does not match' do
+ other_market = create(:market)
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', market: other_market)
+ expect(rule.applicable?(context)).to be false
+ end
+ end
+ end
+end