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.

Fix: Bulk price writes now reindex products for search (#14867)

Damian Legawiec19157d48f19c
diff --git a/spree/core/app/jobs/spree/search_provider/index_job.rb b/spree/core/app/jobs/spree/search_provider/index_job.rb
index 1969834dce..44d1233002 100644
--- a/spree/core/app/jobs/spree/search_provider/index_job.rb
+++ b/spree/core/app/jobs/spree/search_provider/index_job.rb
@@ -10,6 +10,10 @@ module Spree
# (ActiveJob handler lookup is reverse-declaration-order).
discard_on ActiveJob::DeserializationError
+ # Bulk writers enqueue this inside their transaction; run before it
+ # commits, the job would index the product's previous prices.
+ self.enqueue_after_transaction_commit = true
+
# @param resource_class [String] e.g. 'Spree::Product'
# @param resource_id [String] always pass as string for UUID support
# @param store_id [String] always pass as string for UUID support
diff --git a/spree/core/app/services/spree/prices/bulk_upsert.rb b/spree/core/app/services/spree/prices/bulk_upsert.rb
index b0d53d45f8..b8ab6c661f 100644
--- a/spree/core/app/services/spree/prices/bulk_upsert.rb
+++ b/spree/core/app/services/spree/prices/bulk_upsert.rb
@@ -415,15 +415,17 @@ module Spree
# `Variant -> Product` `touch:` chains otherwise.
#
# We use `touch_all` rather than per-record `Variant#touch`: it bumps
- # every affected row in a single UPDATE and skips AR callbacks, giving
- # the cache bust without extra side effects.
+ # every affected row in a single UPDATE. It also skips the product's
+ # commit callbacks, so the search index, which stores prices, is
+ # refreshed here.
def touch_variants(variant_ids)
return if variant_ids.empty?
variants = Spree::Variant.where(id: variant_ids)
- product_ids = variants.pluck(:product_id).uniq
+ products = Spree::Product.where(id: variants.select(:product_id))
variants.touch_all
- Spree::Product.where(id: product_ids).touch_all if product_ids.any?
+ products.touch_all
+ products.find_each(&:enqueue_search_index)
end
# MySQL infers conflict targets from its own unique indexes and
diff --git a/spree/core/app/workflows/spree/price_lists/update.rb b/spree/core/app/workflows/spree/price_lists/update.rb
index 97ac886952..0696ae3917 100644
--- a/spree/core/app/workflows/spree/price_lists/update.rb
+++ b/spree/core/app/workflows/spree/price_lists/update.rb
@@ -54,17 +54,6 @@ module Spree
rows = price_rows
return if rows.empty?
- # A blank row only matters when it clears an amount the list held at
- # that exact rung.
- cleared_keys = rows.select { |row| row[:amount].blank? }.to_set do |row|
- [row[:variant_id].to_s, row[:currency], (row[:min_quantity].presence || 1).to_i]
- end
- stored_rungs = price_list.prices.where(variant_id: cleared_keys.map(&:first)).where.not(amount: nil)
- .pluck(:variant_id, :currency, :min_quantity)
- previously_priced_ids = stored_rungs.filter_map do |variant_id, currency, quantity|
- variant_id if cleared_keys.include?([variant_id.to_s, currency, quantity])
- end
-
# The service refuses a batch that would take a ladder past the break
# cap. Ignoring that would answer 200 while writing nothing
# (docs/plans/6.0-volume-pricing.md).
@@ -89,9 +78,6 @@ module Spree
end
return failure(price_list)
end
-
- priced_ids = rows.select { |row| row[:amount].present? }.map { |row| row[:variant_id] }
- touch_variants((priced_ids + previously_priced_ids).map(&:to_s).uniq)
end
def clear_prices
diff --git a/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb b/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
index 64c4c99130..76737d007f 100644
--- a/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
+++ b/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
@@ -107,6 +107,14 @@ RSpec.describe Spree::Prices::BulkUpsert do
end
end
+ it 'reindexes the product for search' do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+
+ expect {
+ described_class.call(rows: [{ variant_id: variant.id, currency: 'USD', amount: '7.77' }])
+ }.to have_enqueued_job(Spree::SearchProvider::IndexJob).with('Spree::Product', product.id.to_s, product.store_id.to_s)
+ end
+
# Regression: the base-row update loop pulls existing rows via
# `WHERE variant_id IN (...) AND currency IN (...)`, which returns
# cross-pairs (variant_a + EUR, variant_b + USD) that the caller never
diff --git a/spree/core/spec/workflows/spree/price_lists/write_spec.rb b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
index d31631b04f..1b95c3f2c6 100644
--- a/spree/core/spec/workflows/spree/price_lists/write_spec.rb
+++ b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
@@ -50,21 +50,6 @@ RSpec.describe 'Spree::PriceLists write workflows' do
expect(price.amount).to eq(5)
end
- it 'touches only the variants whose prices change' do
- priced_variant, blank_variant = create_list(:variant, 2, product: product)
- price_list.add_products([product.id])
-
- expect {
- described_class.call(
- price_list: price_list,
- attributes: { prices: [
- { variant_id: priced_variant.id, currency: 'USD', amount: 5 },
- { variant_id: blank_variant.id, currency: 'USD', amount: '' }
- ] }
- )
- }.to have_enqueued_job(Spree::Variants::TouchJob).with([priced_variant.id.to_s])
- end
-
# An empty array means "clear every override", which is a different
# instruction from sending no prices at all.
it 'clears every override when given an empty prices array' do