Vysakh
Vysakh

Reputation: 1

How to remove a product from a category API wise in Demandware(sfcc)?

I'm using the code below to remove the product from catelog - skincare ,

var name=pdict.categoryName;  
var category=CatalogMgr.getCategory('skincare'); 

if(category && category.products.length){
    for each(var product in category.products){
        if(!product.custom.productBadge1){
              var assignments = product.getCategoryAssignments();
              
              for(var i=0;i<assignments.length;i++)  
                  {
                  if(assignments[i].category.ID=='skincare'){
                     //TO DO remove product from category
                  }
                  } 

        }
    }
}

which API to be used here

Upvotes: -1

Views: 292

Answers (2)

glorycares
glorycares

Reputation: 1

To remove a product from a category via Demandware (SFCC) API:

  1. Load the Category: Use dw.catalog.CategoryMgr.getCategory(categoryID) to get the category object.
  2. Load the Product: Use dw.catalog.ProductMgr.getProduct(productID) to get the product object.
  3. Remove the Assignment: Use category.removeProduct(product) to remove the product from the category.
  4. Commit Changes: Ensure the operation is done within a transactional context using dw.system.Transaction.wrap().

Example:

output:
const Transaction = require('dw/system/Transaction');
const CategoryMgr = require('dw/catalog/CategoryMgr');
const ProductMgr = require('dw/catalog/ProductMgr');

Transaction.wrap(() => {
const category = CategoryMgr.getCategory('categoryID');
    const product = ProductMgr.getProduct('productID');
    if (category && product) {
        category.removeProduct(product);
    }
});

Upvotes: 0

Albin Joseph
Albin Joseph

Reputation: 1

Well, one possible way is, you can write an XML to remove the assignment and import it manually or through a job. This can be the worst-case scenario.

Upvotes: 0

Related Questions