addProductsToCollection( )


Adds products by ID to a product collection.

The addProductsToCollection() function returns a Promise that resolves when the products with the given IDs are added to a product collection with a given ID.

You can add multiple products to a collection at one time by delimiting the list of products with commas.

With this function, you can only add existing products to a collection. You cannot use the addProductsToCollection() function to create a product. See createProduct() to add a product to the store.

Method Declaration
Copy
function addProductsToCollection(
  collectionId: string,
  productIds: Array<string>,
): Promise<void>;
Method Parameters
collectionIdstringRequired

ID of the product collection to which to add products.


productIdsArray<string>Required

IDs of the products to add to the product collection, separated by commas.

Add products to a product collection
JavaScript
/******************************* * Backend code - products.jsw * *******************************/ import wixStoresBackend from 'wix-stores-backend'; export function addProductsToCollection(collectionId, productIds) { return wixStoresBackend.addProductsToCollection(collectionId, productIds); } /************* * Page code * *************/ import { addProductsToCollection } from 'backend/products'; // ... const collectionId = ... // get collection ID const productIds = ["id1", "id2", "id3"]; addProductsToCollection(collectionId, productIds) .then(() => { // products added to the collection }) .catch((error) => { // products not added to the collection });
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?