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
Refactor wholesale price seeding to use price_list API (#14345)
Damian Legawiec56e606b0c29f
diff --git a/spree/core/db/sample_data/wholesale.rb b/spree/core/db/sample_data/wholesale.rbindex 580d58ac88..7fbe5804a7 100644--- a/spree/core/db/sample_data/wholesale.rb+++ b/spree/core/db/sample_data/wholesale.rb@@ -43,29 +43,29 @@ unless price_list.price_rules.any? { |rule| rule.is_a?(Spree::PriceRules::Customrule.save!end-if price_list.prices.none?- currency = store.default_currency- now = Time.current- rows = Spree::Variant.joins(:product)- .where(spree_products: { store_id: store.id })- .joins(:prices)- .where(spree_prices: { currency: currency, price_list_id: nil })- .pluck(:id, Spree::Price.arel_table[:amount])- .to_h # one row per variant — last base price wins- .filter_map do |variant_id, amount|+# Same paths the admin UI uses: add_products materializes a blank row per+# variant × supported currency (skipping rows that already exist), then trade+# prices (40% off that currency's own retail price) fill in wherever a base+# price exists and the row is still blank. Both steps are idempotent, so+# re-running sample data converges partial state — new products, newly+# supported currencies, or a previously interrupted run — without clobbering+# hand-edited amounts.+price_list.add_products(store.products.ids)++blank_prices = price_list.prices.where(amount: nil).pluck(:id, :variant_id, :currency)+if blank_prices.any?+ base_amounts = Spree::Price.where(price_list_id: nil, variant_id: blank_prices.map { |_, variant_id, _| variant_id }.uniq)+ .pluck(:variant_id, :currency, :amount)+ .each_with_object({}) { |(variant_id, currency, amount), memo| memo[[variant_id, currency]] = amount }++ rows = blank_prices.filter_map do |id, variant_id, currency|+ amount = base_amounts[[variant_id, currency]]next if amount.blank?- {- variant_id: variant_id,- price_list_id: price_list.id,- amount: (amount * 0.6).round(2),- currency: currency,- created_at: now,- updated_at: now- }+ { id: id, variant_id: variant_id, currency: currency, amount: (amount * 0.6).round(2) }end- Spree::Price.insert_all(rows) if rows.any?+ price_list.bulk_update_prices(rows) if rows.any?endprice_list.activate if price_list.can_activate?diff --git a/spree/core/spec/services/spree/sample_data/loader_spec.rb b/spree/core/spec/services/spree/sample_data/loader_spec.rbindex 696a1ceb66..17c371e2dd 100644--- a/spree/core/spec/services/spree/sample_data/loader_spec.rb+++ b/spree/core/spec/services/spree/sample_data/loader_spec.rb@@ -62,6 +62,22 @@ RSpec.describe Spree::SampleData::Loader, type: :service, without_global_store:expect(volume_rule.preferred_min_quantity).to eq(10)end+ it 'seeds wholesale prices for every supported currency' do+ price_list = store.price_lists.find_by(name: 'Wholesale')+ supported = store.supported_currencies_list.map(&:iso_code)+ eligible_variant_ids = Spree::Variant.eligible.where(product_id: store.product_ids).pluck(:id)++ supported.each do |currency|+ expect(price_list.prices.where(currency: currency).pluck(:variant_id)).to match_array(eligible_variant_ids)+ end++ wholesale_price = price_list.prices.where(currency: 'EUR').where.not(amount: nil).first+ expect(wholesale_price).to be_present++ base_price = Spree::Price.find_by(price_list_id: nil, variant_id: wholesale_price.variant_id, currency: 'EUR')+ expect(wholesale_price.amount).to eq((base_price.amount * 0.6).round(2))+ end+it 'mints a wholesale-bound publishable key' doexpect(store.api_keys.active.publishable.where(channel: wholesale)).to existend