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.

Added `Price#remove_compare_at_amount_if_equals_amount` callback to avoid having compare at amount same as regular price

Damian Legawiec4705f0172e2b
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb
index 9495372149..fb0d638cc2 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -12,6 +12,7 @@ module Spree
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices, touch: true
before_validation :ensure_currency
+ before_save :remove_compare_at_amount_if_equals_amount
validates :amount, allow_nil: true, numericality: {
greater_than_or_equal_to: 0,
@@ -112,5 +113,10 @@ module Spree
def ensure_currency
self.currency ||= Spree::Store.default.default_currency
end
+
+ # removes the compare at amount if it is the same as the amount
+ def remove_compare_at_amount_if_equals_amount
+ self.compare_at_amount = nil if compare_at_amount == amount
+ end
end
end
diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb
index 85a79af68f..21e98659cf 100644
--- a/core/spec/models/spree/price_spec.rb
+++ b/core/spec/models/spree/price_spec.rb
@@ -1,6 +1,27 @@
require 'spec_helper'
describe Spree::Price, type: :model do
+ describe 'Callbacks' do
+ context 'when compare_at_amount is equal to amount' do
+ let(:variant) { create(:variant) }
+
+ let(:price) do
+ build(
+ :price,
+ amount: 10,
+ compare_at_amount: 10,
+ currency: 'GBP',
+ variant: variant
+ )
+ end
+
+ it 'sets compare_at_amount to nil' do
+ price.save
+ expect(price.compare_at_amount).to be_nil
+ end
+ end
+ end
+
describe '#amount=' do
let(:price) { build :price }
let(:amount) { '3,0A0' }