Developer forum

Forum » Development » Removing a product from a group

Removing a product from a group

Bo Møller
Reply
Hey guys,

What's the correct way to remove a product from a group, when I have the product object?
Alternatively, I can just remove all products from a group - if you don't know a good way to solve the first problem.
A second alternative - removing a product from all groups it's associated with. How's that done?

Looking forward to hearing from you!

// Bo 

Replies

 
Dmitry Nikolenko
Reply
 Hi Bo,

You should to use ProductGroupRelation object. 
You just need to make reverse operations :)
 
Vilius Janulis
Reply

Hi Dmitry, maybe You could help little bit more.

I am trying to
make same thing, remove product from the group. I have tryed to do it in
any logical way i could think of but could not make it work.

Here are few examples of my code :

Dynamicweb.eCommerce.Products.GroupCollection goupe1 = Dynamicweb.eCommerce.Products.Group.getGroups("GROUP251");
   Dynamicweb.eCommerce.Products.ProductCollection prodCol = Dynamicweb.eCommerce.Products.Product.getProductsFromGroup(goupe1.get_Item(0));

   foreach (Product prod in prodCol)
   {
     Dynamicweb.eCommerce.Products.ProductGroupRelationCollection pgrc = Dynamicweb.eCommerce.Products.ProductGroupRelation.getProductGroupRelations(prod);
     foreach (ProductGroupRelation pgr in pgrc)
     {
        pgr.Delete();
        importStuff.AppendLine("Product with ID : "+ pgr.ProductID + " was deleted from group with ID : "+ pgr.GroupID);
     }
   }


another:

Dynamicweb.eCommerce.Products.GroupRelationCollection groupRelColection = Dynamicweb.eCommerce.Products.GroupRelation.getGroupRelations("GROUP251");
    foreach(GroupRelation groupRelation in groupRelColection){
         groupRelation.Delete();
         importStuff.AppendLine("3");
    }

few mores:
 Dynamicweb.eCommerce.Products.GroupCollection groupCol2 = Dynamicweb.eCommerce.Products.Group.getGroupRelations("GROUP251", "GROUP251");
      Dynamicweb.eCommerce.Products.ProductGroupRelationCollection prodRel2 = Dynamicweb.eCommerce.Products.ProductGroupRelation.getProductGroupRelations(p.ID, "GROUP251");
      Dynamicweb.eCommerce.Products.ProductGroupRelation.getProductGroupRelations(p.ID, "GROUP251").Clear();
      Dynamicweb.eCommerce.Products.GroupCollection groupCol = Dynamicweb.eCommerce.Products.Group.getGroupsBySearch("GROUP251",1);
      foreach(Group group in groupCol){
         if (groupCol2.ContainsById("GROUP251"))
         {    
           groupCol2.Remove(group);
         } importStuff.AppendLine("1");
      }
      foreach (ProductGroupRelation rel2 in prodRel2)
      {
          rel2.Delete();
          rel2.Delete(importedProducts);
          //rel2.Delete(importedProducts,"GROUP251");
          importStuff.AppendLine("2");
      }

Of course i have left out
the examples that seemed to be most obvious : like remove from group
methods and etc. Can You or someone help us with this one ?
 
Yury Zhukovskiy
Reply

Hi Vilius, Dmirty is at vacation.

To delete a product from a group you need to execute next code:

eCommerce.Products.ProductDeleting.Run("PROD881", "GROUP10", eCommerce.Common.Context.LanguageID)

It will remove product (id = PROD188) from a group (id=GROUP10).

 

Kind Regards

Zhukovskiy Yury

 
Lanzo van Slooten
Reply

Who has the solution? because all the above does not work with me!!

Best regards,
Lanzo van Slooten
 
Morten Bengtson
Reply
Try this...

// Get the product.
var product = Dynamicweb.eCommerce.Products.Product.Create("PROD1");

// Get the group relations for that product.
var relations = Dynamicweb.eCommerce.Products.ProductGroupRelation.getProductGroupRelations(product);

// Loop and delete relations.
foreach (Dynamicweb.eCommerce.Products.ProductGroupRelation relation in relations)
{
    relation.Delete(); // Contains a bug and does not work at all!
    relation.Delete(relation.ProductID, relation.GroupID); // You need to pass the correct id for product and group.
}

// Alternative approach...
var rel = new Dynamicweb.eCommerce.Products.ProductGroupRelation();
rel.Delete(product.ID, "GROUP1"); // Delete relation to a specific group.
rel.Delete(product.ID, string.Empty); // Delete relation to all groups.

 

You must be logged in to post in the forum