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
feat(api): add bulk add/remove products to collections
Łukasz Adamczyk124de28f5f6a
diff --git a/docs/plans/6.0-replace-taxons-with-categories.md b/docs/plans/6.0-replace-taxons-with-categories.mdindex e2d6a813bc..8a3ae976ce 100644--- a/docs/plans/6.0-replace-taxons-with-categories.md+++ b/docs/plans/6.0-replace-taxons-with-categories.md@@ -403,6 +403,10 @@ POST /api/v3/admin/collections/:id/productsDELETE /api/v3/admin/collections/:id/products/:product_idPATCH /api/v3/admin/collections/:id/products/:product_id/reposition# (+ nested custom_fields and translations via concerns)+#+# Bulk product membership (mirrors the category bulk endpoints):+POST /api/v3/admin/products/bulk_add_to_collections # { ids: [...], collection_ids: [...] }+POST /api/v3/admin/products/bulk_remove_from_collections # { ids: [...], collection_ids: [...] }```Unlike categories (a nested set that needs an explicit `reposition` with a target parent + index), collections are a flat `acts_as_list`, so the collection's own order is reordered by sending `position` on `PATCH /collections/:id` — matching the SPA `<ResourceTable reorder>` convention and the sibling flat resource (`payment_methods`). Only the **nested** membership (`ProductCollection.position`) keeps a dedicated `reposition`. Admin collection endpoints require the new `read_collections` / `write_collections` API-key scopes (JWT admins use CanCanCan as usual).diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rbindex a0e62330ab..8f71b24e40 100644--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb@@ -11,6 +11,8 @@ module Spree:bulk_status_update,:bulk_add_to_categories,:bulk_remove_from_categories,+ :bulk_add_to_collections,+ :bulk_remove_from_collections,:bulk_add_to_channels,:bulk_remove_from_channels,:bulk_destroy@@ -61,6 +63,18 @@ module Spreeapply_categories(Spree::Taxons::RemoveProducts)end+ # POST /api/v3/admin/products/bulk_add_to_collections+ # Body: { ids: [...], collection_ids: [...] }+ def bulk_add_to_collections+ apply_collections(Spree::Collections::AddProducts)+ end++ # POST /api/v3/admin/products/bulk_remove_from_collections+ # Body: { ids: [...], collection_ids: [...] }+ def bulk_remove_from_collections+ apply_collections(Spree::Collections::RemoveProducts)+ end+# POST /api/v3/admin/products/bulk_add_to_channels# Body: { ids: [...], channel_ids: [...] }def bulk_add_to_channels@@ -225,6 +239,18 @@ module Spreerender json: { product_count: bulk_collection.size, category_count: categories.size }end+ def apply_collections(service)+ authorize! :update, model_class++ collection_ids = decode_ids(params[:collection_ids])+ collections = current_store.collections.+ accessible_by(current_ability, :update).where(id: collection_ids)++ service.call(collections: collections, products: bulk_collection)++ render json: { product_count: bulk_collection.size, collection_count: collections.size }+ end+def scoped_channelschannel_ids = decode_ids(params[:channel_ids])current_store.channels.accessible_by(current_ability, :manage).where(id: channel_ids)diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex df8d3ac531..f0574e02d6 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -199,6 +199,8 @@ Spree::Core::Engine.add_routes dopost :bulk_status_updatepost :bulk_add_to_categoriespost :bulk_remove_from_categories+ post :bulk_add_to_collections+ post :bulk_remove_from_collectionspost :bulk_add_to_channelspost :bulk_remove_from_channelspost :bulk_add_tagsdiff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rbindex f8e0eb4614..77a2483dee 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb@@ -1291,6 +1291,80 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller doendend+ describe 'POST #bulk_add_to_collections' do+ let!(:collection) { create(:collection, store: store) }+ let!(:other_collection) { create(:collection, store: store) }+ let!(:second_product) { create(:product) }++ before { request.headers.merge!(headers) }++ it 'adds every product to every collection' do+ post :bulk_add_to_collections, params: {+ ids: [product.prefixed_id, second_product.prefixed_id],+ collection_ids: [collection.prefixed_id, other_collection.prefixed_id]+ }, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response).to eq('product_count' => 2, 'collection_count' => 2)+ expect(product.reload.collections).to include(collection, other_collection)+ expect(second_product.reload.collections).to include(collection, other_collection)+ end++ it 'silently ignores collections from other stores' do+ foreign_collection = create(:collection, store: create(:store))++ post :bulk_add_to_collections, params: {+ ids: [product.prefixed_id],+ collection_ids: [collection.prefixed_id, foreign_collection.prefixed_id]+ }, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response['collection_count']).to eq(1)+ expect(product.reload.collections).to include(collection)+ expect(product.reload.collections).not_to include(foreign_collection)+ end++ it 'silently drops products from other stores' do+ other_store_product = create(:product, store: create(:store))++ post :bulk_add_to_collections, params: {+ ids: [product.prefixed_id, other_store_product.prefixed_id],+ collection_ids: [collection.prefixed_id]+ }, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response['product_count']).to eq(1)+ expect(other_store_product.reload.collections).to be_empty+ end++ it 'returns 422 when ids is missing' do+ post :bulk_add_to_collections, params: { collection_ids: [collection.prefixed_id] }, as: :json+ expect(response).to have_http_status(:unprocessable_content)+ end+ end++ describe 'POST #bulk_remove_from_collections' do+ let!(:collection) { create(:collection, store: store) }+ let!(:second_product) { create(:product) }++ before do+ request.headers.merge!(headers)+ Spree::Collections::AddProducts.call(collections: [collection], products: [product, second_product])+ end++ it 'removes every product from every collection' do+ post :bulk_remove_from_collections, params: {+ ids: [product.prefixed_id, second_product.prefixed_id],+ collection_ids: [collection.prefixed_id]+ }, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response).to eq('product_count' => 2, 'collection_count' => 1)+ expect(product.reload.collections).not_to include(collection)+ expect(second_product.reload.collections).not_to include(collection)+ end+ end+describe 'POST #bulk_add_to_categories' dolet(:taxonomy) { create(:taxonomy, store: store) }let(:category) { create(:taxon, taxonomy: taxonomy) }