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.

Expire Price List cache key when adding/removing products

Damian Legawiece682bd0f1953
diff --git a/core/app/models/spree/price_list.rb b/core/app/models/spree/price_list.rb
index 291c7d0865..9a21d4dce3 100644
--- a/core/app/models/spree/price_list.rb
+++ b/core/app/models/spree/price_list.rb
@@ -155,6 +155,7 @@ module Spree
# Use upsert_all with on_duplicate: :skip to handle race conditions
Spree::Price.upsert_all(prices_to_insert, on_duplicate: :skip)
touch_variants(variant_ids)
+ touch
end
# Removes products from the price list
@@ -165,10 +166,13 @@ module Spree
return if product_ids.blank?
variant_ids = Spree::Variant.where(product_id: product_ids).distinct.pluck(:id)
+ return if variant_ids.empty?
+
# Use delete_all for hard delete - this bypasses acts_as_paranoid
# which is required for the unique index to work when re-adding products
prices.where(variant_id: variant_ids).delete_all
touch_variants(variant_ids)
+ touch
end
# Bulk update prices using upsert_all for performance
diff --git a/core/spec/models/spree/price_list_spec.rb b/core/spec/models/spree/price_list_spec.rb
index b5ac11c6a2..238912f224 100644
--- a/core/spec/models/spree/price_list_spec.rb
+++ b/core/spec/models/spree/price_list_spec.rb
@@ -212,6 +212,12 @@ describe Spree::PriceList, type: :model do
}.to have_enqueued_job(Spree::Variants::TouchJob).with([product1.master.id])
end
+ it 'touches the price list to bust cache' do
+ expect {
+ price_list.add_products([product1.id])
+ }.to change { price_list.reload.updated_at }
+ end
+
it 'handles empty product_ids' do
expect {
price_list.add_products([])
@@ -342,6 +348,12 @@ describe Spree::PriceList, type: :model do
}.to have_enqueued_job(Spree::Variants::TouchJob).with([product1.master.id])
end
+ it 'touches the price list to bust cache' do
+ expect {
+ price_list.remove_products([product1.id])
+ }.to change { price_list.reload.updated_at }
+ end
+
context 'when re-adding a previously removed product' do
it 'allows re-adding a product with empty prices' do
# Add product, then remove it