Developer forum

Forum » Development » Delete GroupProduct relation using Services?

Delete GroupProduct relation using Services?

René Benjaminsen
Reply

Hello how do i delete the EcomGroupProductRelation when deleting a product? my code:

        public void DeleteProduct(string id)
        {
            var products = new ProductCollection();
            var product = _productService.GetProductById(id, null, true) ?? throw new ArgumentNullException("DeleteProduct: productId NotFound");

            products.Add(product);
            _productService.DeleteProducts(products);
        }

this deletes the product/products but not the group product relation..


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

You can use the GroupService - it has a DeleteRelation method.

See https://doc.dynamicweb.com/apix/api/Dynamicweb.Ecommerce.Products.GroupService.html#Dynamicweb_Ecommerce_Products_GroupService_DeleteRelation_System_String_System_String_

BR Nicolai

Votes for this answer: 2
 
René Benjaminsen
Reply

Okay, so something like this

            var product = _productService.GetProductById(id, null, true) ?? throw new ArgumentNullException("DeleteProduct: productId couldent be found");

            foreach (var group in product.Groups)
            {
                _groupService.DeleteRelation(product.Id, group.Id);
            }
            _productService.Delete(product.Id);

thanks for the answer :)

 

You must be logged in to post in the forum