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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 9495372149..fb0d638cc2 100644--- a/core/app/models/spree/price.rb+++ b/core/app/models/spree/price.rb@@ -12,6 +12,7 @@ module Spreebelongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices, touch: truebefore_validation :ensure_currency+ before_save :remove_compare_at_amount_if_equals_amountvalidates :amount, allow_nil: true, numericality: {greater_than_or_equal_to: 0,@@ -112,5 +113,10 @@ module Spreedef ensure_currencyself.currency ||= Spree::Store.default.default_currencyend++ # 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+ endendenddiff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rbindex 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=' dolet(:price) { build :price }let(:amount) { '3,0A0' }