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.

Auto match product taxons after the product price changes (#12246)

Łukasz Adamczyk3cd90d5e68c6
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb
index e6f5b7c540..34d9094431 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -43,6 +43,10 @@ module Spree
self.whitelisted_ransackable_attributes = ['amount', 'compare_at_amount']
+ attribute :eligible_for_taxon_matching, :boolean, default: false
+ before_validation -> { self.eligible_for_taxon_matching = new_record? ? discounted? : discounted? != was_discounted? }
+ after_commit -> { variant&.product&.auto_match_taxons }, if: -> { eligible_for_taxon_matching? }
+
def money
Spree::Money.new(amount || 0, currency: currency.upcase)
end
diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb
index 77c6e00bd2..7ba96d3616 100644
--- a/core/spec/models/spree/price_spec.rb
+++ b/core/spec/models/spree/price_spec.rb
@@ -20,6 +20,66 @@ describe Spree::Price, type: :model do
expect(price.compare_at_amount).to be_nil
end
end
+
+ describe 'after_commit :auto_match_taxons' do
+ context 'when price is discounted' do
+ context 'on create' do
+ let(:price) { build(:price, amount: 10, compare_at_amount: 20, currency: 'GBP') }
+
+ it 'auto matches taxons' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons).at_least(:once)
+ price.save
+ end
+ end
+
+ context 'on update' do
+ let!(:price) { create(:price, amount: 10, compare_at_amount: 20, currency: 'GBP') }
+
+ context 'and changed to not be discounted' do
+ it 'auto matches taxons' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ price.reload.update(compare_at_amount: nil)
+ end
+ end
+
+ context 'and is still discounted' do
+ it 'does not touch shop product' do
+ expect_any_instance_of(Spree::Product).not_to receive(:auto_match_taxons)
+ price.reload.update(amount: 15)
+ end
+ end
+ end
+ end
+
+ context 'when price is not discounted' do
+ let(:price) { build(:price, amount: 10, compare_at_amount: nil, currency: 'GBP') }
+
+ context 'on create' do
+ it 'auto matches taxons' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ price.save
+ end
+ end
+
+ context 'on update' do
+ let!(:price) { create(:price, amount: 10, compare_at_amount: nil, currency: 'GBP') }
+
+ context 'and changed to be discounted' do
+ it 'auto matches taxons' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ price.reload.update(compare_at_amount: 20)
+ end
+ end
+
+ context 'and is still not discounted' do
+ it 'does not touch shop product' do
+ expect_any_instance_of(Spree::Product).not_to receive(:auto_match_taxons)
+ price.reload.update(amount: 15)
+ end
+ end
+ end
+ end
+ end
end
describe '#amount=' do