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.

Remove redundant price rules

Damian Legawiec512201ae31a6
diff --git a/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb
index abc23604a9..99ad677ea1 100644
--- a/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb
@@ -37,11 +37,6 @@ describe Spree::V2::Storefront::ProductSerializer do
let(:other_store) { create(:store, default: false) }
let(:other_zone) { create(:zone) }
let(:user) { create(:user) }
- let(:taxon) { create(:taxon) }
-
- before do
- product.taxons << taxon
- end
context 'with store-specific pricing' do
let!(:price_list) { create(:price_list, :active, store: other_store) }
@@ -132,17 +127,6 @@ describe Spree::V2::Storefront::ProductSerializer do
end
end
- context 'with taxon-specific pricing' do
- let!(:price_list) { create(:price_list, :active, store: store) }
- let!(:taxon_rule) { create(:product_taxon_price_rule, price_list: price_list, taxon_ids: [taxon.id]) }
- let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 11.00, price_list: price_list) }
-
- it 'returns the taxon-specific price' do
- expect(subject[:data][:attributes][:price]).to eq(BigDecimal('11.00'))
- expect(subject[:data][:attributes][:display_price]).to eq('$11.00')
- end
- end
-
context 'with combined rules (zone AND user)' do
let!(:price_list) { create(:price_list, :active, store: store, match_policy: 'all') }
let!(:zone_rule) { create(:zone_price_rule, price_list: price_list, zone_ids: [other_zone.id]) }
diff --git a/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb
index 291d336120..02b80f9faf 100644
--- a/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb
@@ -150,7 +150,6 @@ describe Spree::V2::Storefront::VariantSerializer do
context 'with date-based pricing' do
let!(:price_list) { create(:price_list, :active, store: store, starts_at: 1.day.ago, ends_at: 1.day.from_now) }
- let!(:date_rule) { create(:date_range_price_rule, price_list: price_list, starts_at: 1.day.ago, ends_at: 1.day.from_now) }
let!(:price_list_price) { create(:price, variant: variant, currency: currency, amount: 7.00, price_list: price_list) }
it 'returns the date-specific price when within range' do
diff --git a/core/app/models/spree/price_rules/date_range_rule.rb b/core/app/models/spree/price_rules/date_range_rule.rb
deleted file mode 100644
index 8b065b6075..0000000000
--- a/core/app/models/spree/price_rules/date_range_rule.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-module Spree
- module PriceRules
- class DateRangeRule < Spree::PriceRule
- preference :starts_at, :datetime
- preference :ends_at, :datetime
-
- def applicable?(context)
- date = context.date || Time.current
- timezone = context.store&.preferred_timezone || 'UTC'
- date = date.in_time_zone(timezone)
-
- return false if preferred_starts_at && date < preferred_starts_at
- return false if preferred_ends_at && date > preferred_ends_at
-
- true
- end
-
- def self.description
- 'Apply pricing within a specific date range'
- end
- end
- end
-end
diff --git a/core/app/models/spree/price_rules/product_taxon_rule.rb b/core/app/models/spree/price_rules/product_taxon_rule.rb
deleted file mode 100644
index ebd15893e9..0000000000
--- a/core/app/models/spree/price_rules/product_taxon_rule.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-module Spree
- module PriceRules
- class ProductTaxonRule < Spree::PriceRule
- preference :taxon_ids, :array, default: []
- preference :include_descendants, :boolean, default: true
-
- def applicable?(context)
- return true if preferred_taxon_ids.empty?
-
- product = context.variant.product
- product_taxon_ids = product.taxons.pluck(:id)
-
- if preferred_include_descendants
- taxon_ids_with_descendants = Spree::Taxon.where(id: preferred_taxon_ids)
- .map { |t| [t.id] + t.descendants.pluck(:id) }
- .flatten
- (product_taxon_ids & taxon_ids_with_descendants).any?
- else
- (product_taxon_ids & preferred_taxon_ids).any?
- end
- end
-
- def self.description
- 'Apply pricing based on product taxon'
- end
- end
- end
-end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 57a3860f3f..4614d4716b 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1688,12 +1688,6 @@ en:
scheduled: Scheduled
price_range: Price Range
price_rule_types:
- date_range_rule:
- description: Apply pricing during specific dates
- name: Date Range
- product_taxon_rule:
- description: Apply pricing to products in specific categories
- name: Product Category
user_rule:
description: Apply pricing to specific customers
name: User
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 6c79734fea..55cca09117 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -166,9 +166,7 @@ module Spree
Rails.application.config.spree.pricing.rules.concat [
'Spree::PriceRules::ZoneRule',
'Spree::PriceRules::UserRule',
- 'Spree::PriceRules::DateRangeRule',
- 'Spree::PriceRules::VolumeRule',
- 'Spree::PriceRules::ProductTaxonRule'
+ 'Spree::PriceRules::VolumeRule'
]
Rails.application.config.spree.promotions.actions = [
diff --git a/core/lib/spree/testing_support/factories/price_rule_factory.rb b/core/lib/spree/testing_support/factories/price_rule_factory.rb
index aa60b81946..bfd0a3ecb5 100644
--- a/core/lib/spree/testing_support/factories/price_rule_factory.rb
+++ b/core/lib/spree/testing_support/factories/price_rule_factory.rb
@@ -13,18 +13,6 @@ FactoryBot.define do
end
end
- factory :date_range_price_rule, class: Spree::PriceRules::DateRangeRule do
- after(:build) do |rule, evaluator|
- rule.preferred_starts_at = evaluator.starts_at if evaluator.starts_at.present?
- rule.preferred_ends_at = evaluator.ends_at if evaluator.ends_at.present?
- end
-
- transient do
- starts_at { nil }
- ends_at { nil }
- end
- end
-
factory :volume_price_rule, class: Spree::PriceRules::VolumeRule do
after(:build) do |rule, evaluator|
rule.preferred_min_quantity = evaluator.min_quantity if evaluator.min_quantity.present?
@@ -39,18 +27,6 @@ FactoryBot.define do
end
end
- factory :product_taxon_price_rule, class: Spree::PriceRules::ProductTaxonRule do
- after(:build) do |rule, evaluator|
- rule.preferred_taxon_ids = evaluator.taxon_ids if evaluator.respond_to?(:taxon_ids)
- rule.preferred_include_descendants = evaluator.include_descendants if evaluator.respond_to?(:include_descendants)
- end
-
- transient do
- taxon_ids { [] }
- include_descendants { true }
- end
- end
-
factory :user_price_rule, class: Spree::PriceRules::UserRule do
after(:build) do |rule, evaluator|
rule.preferred_user_ids = evaluator.user_ids if evaluator.respond_to?(:user_ids)
diff --git a/core/spec/models/spree/price_list_spec.rb b/core/spec/models/spree/price_list_spec.rb
index c0848ff729..9f7ac53aa4 100644
--- a/core/spec/models/spree/price_list_spec.rb
+++ b/core/spec/models/spree/price_list_spec.rb
@@ -131,18 +131,15 @@ describe Spree::PriceList, type: :model do
end
context 'with rules and match_policy = all' do
- let!(:passing_rule) do
- rule = create(:zone_price_rule, price_list: price_list)
- allow(rule).to receive(:applicable?).and_return(true)
- rule
- end
- let!(:failing_rule) do
- rule = create(:date_range_price_rule, price_list: price_list)
- allow(rule).to receive(:applicable?).and_return(false)
- rule
- end
+ let!(:passing_rule) { create(:zone_price_rule, price_list: price_list) }
+ let!(:failing_rule) { create(:user_price_rule, price_list: price_list) }
- before { price_list.update(match_policy: 'all') }
+ before do
+ price_list.update(match_policy: 'all')
+ price_list.price_rules.reload
+ allow(price_list.price_rules.first).to receive(:applicable?).and_return(true)
+ allow(price_list.price_rules.second).to receive(:applicable?).and_return(false)
+ end
it 'returns false if any rule fails' do
expect(price_list.applicable?(context)).to be false
@@ -150,18 +147,15 @@ describe Spree::PriceList, type: :model do
end
context 'with rules and match_policy = any' do
- let!(:passing_rule) do
- rule = create(:zone_price_rule, price_list: price_list)
- allow(rule).to receive(:applicable?).and_return(true)
- rule
- end
- let!(:failing_rule) do
- rule = create(:date_range_price_rule, price_list: price_list)
- allow(rule).to receive(:applicable?).and_return(false)
- rule
- end
+ let!(:passing_rule) { create(:zone_price_rule, price_list: price_list) }
+ let!(:failing_rule) { create(:user_price_rule, price_list: price_list) }
- before { price_list.update(match_policy: 'any') }
+ before do
+ price_list.update(match_policy: 'any')
+ price_list.price_rules.reload
+ allow(price_list.price_rules.first).to receive(:applicable?).and_return(true)
+ allow(price_list.price_rules.second).to receive(:applicable?).and_return(false)
+ end
it 'returns true if any rule passes' do
expect(price_list.applicable?(context)).to be true
diff --git a/core/spec/models/spree/price_rules/date_range_rule_spec.rb b/core/spec/models/spree/price_rules/date_range_rule_spec.rb
deleted file mode 100644
index 3766904b10..0000000000
--- a/core/spec/models/spree/price_rules/date_range_rule_spec.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-require 'spec_helper'
-
-describe Spree::PriceRules::DateRangeRule, type: :model do
- let(:price_list) { create(:price_list) }
- let(:rule) { create(:date_range_price_rule, price_list: price_list) }
- let(:variant) { create(:variant) }
-
- describe '#applicable?' do
- context 'with starts_at and ends_at set' do
- before do
- rule.preferred_starts_at = 1.day.ago
- rule.preferred_ends_at = 1.day.from_now
- end
-
- it 'returns true when date is within range' do
- context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: Time.current)
- expect(rule.applicable?(context)).to be true
- end
-
- it 'returns false when date is before range' do
- context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: 2.days.ago)
- expect(rule.applicable?(context)).to be false
- end
-
- it 'returns false when date is after range' do
- context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: 2.days.from_now)
- expect(rule.applicable?(context)).to be false
- end
- end
- end
-end
diff --git a/core/spec/models/spree/price_rules/product_taxon_rule_spec.rb b/core/spec/models/spree/price_rules/product_taxon_rule_spec.rb
deleted file mode 100644
index 4a8a214fea..0000000000
--- a/core/spec/models/spree/price_rules/product_taxon_rule_spec.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-require 'spec_helper'
-
-describe Spree::PriceRules::ProductTaxonRule, type: :model do
- let(:price_list) { create(:price_list) }
- let(:rule) { create(:product_taxon_price_rule, price_list: price_list) }
- let(:taxon) { create(:taxon) }
- let(:product) { create(:product, taxons: [taxon]) }
- let(:variant) { product.master }
-
- describe '#applicable?' do
- context 'when taxon_ids preference is empty' do
- before { rule.preferred_taxon_ids = [] }
-
- it 'returns true for any product' do
- context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')
- expect(rule.applicable?(context)).to be true
- end
- end
-
- context 'when taxon_ids preference is set' do
- before { rule.preferred_taxon_ids = [taxon.id] }
-
- it 'returns true when product has matching taxon' do
- context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')
- expect(rule.applicable?(context)).to be true
- end
-
- it 'returns false when product does not have matching taxon' do
- other_product = create(:product)
- context = Spree::Pricing::Context.new(variant: other_product.master, currency: 'USD')
- expect(rule.applicable?(context)).to be false
- end
- end
- end
-end