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
Treat a blank price amount as no price in that currency
Damian Legawiec25d7e52d181f
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex ff35046fdd..14940d4874 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -526,14 +526,31 @@ module Spree# observe the write without a reload. `base_prices.find_or_initialize_by`# would issue a fresh query and return a detached object, leaving the# loaded collection — and the serialized response — stale.- price =+ existing =if prices.loaded?- prices.detect { |p| p.price_list_id.nil? && p.currency == currency } ||- prices.build(currency: currency)+ prices.detect { |p| p.price_list_id.nil? && p.currency == currency }else- prices.base_prices.find_or_initialize_by(currency: currency)+ prices.base_prices.find_by(currency: currency)end+ # A blank amount means the merchant left that currency empty, which is+ # the same statement as leaving it out of the payload: there is no base+ # price in this currency. Writing it would persist a nil amount, and a+ # base price (unlike a price-list placeholder) must have one.+ if amount.blank?+ return if existing.nil?++ prices.delete(existing) if prices.loaded?+ existing.destroy if existing.persisted?+ return+ end++ price = existing || if prices.loaded?+ prices.build(currency: currency)+ else+ prices.base_prices.build(currency: currency)+ end+price.amount = amountprice.compare_at_amount = compare_at_amountprice.save! if persisted?diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rbindex fbe1c8eb0a..52d1cc71ab 100644--- a/spree/core/spec/models/spree/variant_spec.rb+++ b/spree/core/spec/models/spree/variant_spec.rb@@ -669,6 +669,39 @@ describe Spree::Variant, type: :model doexpect(price.compare_at_amount).to eq(35.00)end+ # A base price must carry an amount, so a currency the merchant left empty+ # is an absent base price rather than one with a nil amount.+ context 'when the amount is blank' do+ it 'does not create a base price' do+ expect { variant.set_price(currency, '') }.not_to raise_error++ expect(variant.prices.base_prices.find_by(currency: currency)).to be_nil+ end++ it 'removes an existing base price' do+ create(:price, variant: variant, currency: currency, amount: 10.00)++ variant.set_price(currency, '')++ expect(variant.prices.base_prices.find_by(currency: currency)).to be_nil+ end++ it 'leaves a price list placeholder alone' do+ price_list = create(:price_list)+ placeholder = create(:price, variant: variant, currency: currency, amount: nil, price_list: price_list)++ variant.set_price(currency, '')++ expect(placeholder.reload).to be_persisted+ end++ it 'does not raise when the prices association is loaded' do+ variant.prices.load++ expect { variant.set_price(currency, '') }.not_to raise_error+ end+ end+# Guards the PATCH /variants response: when the prices association is# eager-loaded for serialization, updating a base price must be visible# to readers that branch on `prices.loaded?` (#price_in, #price_for)