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.

Keep a catalog's price list in step with its assortment

Damian Legawiec460baea9a40f
diff --git a/docs/plans/6.0-catalog-agreement-rework.md b/docs/plans/6.0-catalog-agreement-rework.md
index b6ff3f041d..700f2fc180 100644
--- a/docs/plans/6.0-catalog-agreement-rework.md
+++ b/docs/plans/6.0-catalog-agreement-rework.md
@@ -4,7 +4,7 @@
**Target:** Spree 6.0 — sequence Wave 1: everything catalog-shaped builds on the inverted binding, and the leak fix touches unreleased code
**Depends on:** B2B Companies & Catalogs (6.0-b2b-companies-and-catalogs.md — the shipped catalogs this reworks), PriceList system (released)
**Author:** Damian + Claude
-**Last updated:** 2026-08-30
+**Last updated:** 2026-08-31
## Summary
@@ -159,6 +159,12 @@ catalog page this rework establishes.
the default catalog.
- Catalog-page features (terms, pricing card) mount on the agreement
editor this plan establishes — no parallel catalog editing surfaces.
+- **An owned list's product membership follows the catalog's assortment**
+ — `Catalog#add_products`/`#remove_products` carry it, and creating an
+ owned list seeds it from what the catalog already holds. Never populate
+ an owned list by another path: the two memberships are separate tables,
+ and a catalog whose list missed the sync shows an empty price
+ spreadsheet while plainly having products.
## Open Questions
diff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
index 45541ad3f5..66d2c47f9d 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
@@ -33,10 +33,10 @@ module Spree
@parent_catalog.add_products(products.map(&:id))
end
- # One statement — the join rows carry no callbacks worth firing.
+ # Through the model, so an owned price list drops the rows it
+ # held for these products along with them.
def remove_member_products(products)
- @parent_catalog.catalog_products.where(product_id: products.map(&:id)).delete_all
- @parent_catalog.touch
+ @parent_catalog.remove_products(products.map(&:id))
end
def set_parent
diff --git a/spree/core/app/models/spree/catalog.rb b/spree/core/app/models/spree/catalog.rb
index 5e822c5cd1..7b751fc735 100644
--- a/spree/core/app/models/spree/catalog.rb
+++ b/spree/core/app/models/spree/catalog.rb
@@ -233,12 +233,42 @@ module Spree
# and rejects an explicit +unique_by+; PostgreSQL/SQLite require it.
opts[:unique_by] = %i[catalog_id product_id] unless mysql_adapter?
- Spree::CatalogProduct.upsert_all(rows, **opts)
- touch
+ # Both memberships move together: a half-applied add is the drift this
+ # sync exists to prevent, leaving the spreadsheet without rows for
+ # products the assortment already lists.
+ transaction do
+ Spree::CatalogProduct.upsert_all(rows, **opts)
+ # An owned list prices this assortment and nothing else, so it
+ # follows it: those placeholder rows are what the spreadsheet edits.
+ price_list&.add_products(new_ids)
+ touch
+ end
new_ids.size
end
+ # Drops products from the assortment, and the rows an owned list carried
+ # for them — a price for something this audience can no longer see is
+ # dead weight, and would reappear if the product were added back.
+ #
+ # @param product_ids [Array<Integer>] raw product PKs
+ # @return [Integer] how many were removed
+ def remove_products(product_ids)
+ return 0 if product_ids.blank?
+
+ removed = 0
+
+ transaction do
+ removed = catalog_products.where(product_id: product_ids).delete_all
+ next if removed.zero?
+
+ price_list&.remove_products(product_ids)
+ touch
+ end
+
+ removed
+ end
+
# Copies the price list's products into the assortment — the explicit
# "the wholesale range is already priced" convenience. Deliberately never
# automatic: an empty assortment is a pricing-only overlay that hides
diff --git a/spree/core/app/workflows/spree/catalogs/set_price_list.rb b/spree/core/app/workflows/spree/catalogs/set_price_list.rb
index 4ccb41bb6f..f4afe8a9f3 100644
--- a/spree/core/app/workflows/spree/catalogs/set_price_list.rb
+++ b/spree/core/app/workflows/spree/catalogs/set_price_list.rb
@@ -60,8 +60,13 @@ module Spree
attributes: list_attributes(for_create: true).merge(catalog_id: catalog.id)
)
return failure(catalog, result.error.value) unless result.success?
+ return failure(catalog) unless catalog.update(price_list: result.value)
- failure(catalog) unless catalog.update(price_list: result.value)
+ # The assortment usually exists before the pricing does — a merchant
+ # picks products, then decides what they cost. The new list starts
+ # from what the catalog already holds, so the spreadsheet has rows
+ # to edit rather than opening empty.
+ result.value.add_products(catalog.catalog_products.pluck(:product_id))
end
# A list a catalog owns is reached only through that catalog, so it
diff --git a/spree/core/spec/models/spree/catalog_spec.rb b/spree/core/spec/models/spree/catalog_spec.rb
index 7da10f208c..5852339ddf 100644
--- a/spree/core/spec/models/spree/catalog_spec.rb
+++ b/spree/core/spec/models/spree/catalog_spec.rb
@@ -28,6 +28,30 @@ describe Spree::Catalog, type: :model do
expect(catalog.products.reload).to contain_exactly(product)
end
+ # An owned list prices the assortment and nothing else, so it follows it
+ # — those placeholder rows are what the price spreadsheet edits.
+ it 'gives an owned price list rows for the products it adds' do
+ list = create(:price_list, store: store)
+ catalog.update!(price_list: list)
+ product = create(:product, store: store, price: 100)
+
+ expect { catalog.add_products([product.id]) }.to change { list.prices.reload.count }.from(0)
+ expect(list.variants).to include(product.default_variant)
+ end
+
+ it 'drops those rows again when the product leaves the assortment' do
+ list = create(:price_list, store: store)
+ catalog.update!(price_list: list)
+ kept = create(:product, store: store, price: 100)
+ dropped = create(:product, store: store, price: 50)
+ catalog.add_products([kept.id, dropped.id])
+
+ expect { catalog.remove_products([dropped.id]) }.to change { list.prices.reload.count }
+
+ expect(list.variants.reload).to include(kept.default_variant)
+ expect(list.variants).not_to include(dropped.default_variant)
+ end
+
it 'appends to the existing assortment in one statement' do
first = create(:product, store: store)
catalog.add_products([first.id])
diff --git a/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb b/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb
index aaddd49e48..f838149d33 100644
--- a/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb
+++ b/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb
@@ -82,6 +82,19 @@ describe Spree::Catalogs::SetPriceList do
expect(Spree::PriceList.where(id: list.id)).to be_present
end
+ # The assortment usually exists before the pricing does: a merchant picks
+ # products, then decides what they cost. Without this the price
+ # spreadsheet opens empty on a catalog that plainly has products.
+ it 'starts a new list from the assortment the catalog already holds' do
+ product = create(:product, store: store, price: 100)
+ catalog.add_products([product.id])
+
+ call(name: 'Fixed')
+
+ expect(catalog.reload.price_list.prices).to be_present
+ expect(catalog.price_list.variants).to include(product.default_variant)
+ end
+
it 'is a no-op when detaching a catalog that owns nothing' do
expect(described_class.call(catalog: catalog, attributes: nil)).to be_success
expect(catalog.reload.price_list).to be_nil