November Happy Hour will be moved to Thursday December 5th.

EpiServer Commerce 11.8 - How to remove existing categories of the product?

Vote:
 

Hi Guys,

I am using EPiServer Commerce 11.8.

I am importing ctalog via Schedule job as their are lot of bespoke fields.

I want to remove the existing assigned categories and then assign mentioned in CSV file.

All I have is category code and product code.

I am very new to this. So a detailed explanation would be a favour. 

Can you please help?

Here is the sample code I have written to GET and DELETE. However the GetCategories does always return 0 categories for given product.

private IList<ContentReference> GetCategories(string productCode)
        {
            var productContentLink = this.referenceConverter.GetContentLink(productCode, CatalogContentType.CatalogEntry);
            IList<ContentReference> categoryRefrences = new List<ContentReference>();

            var categories = this.contentLoader.GetChildren<CategoryNode>(productContentLink);

            foreach (var category in categories)
            {
                System.Diagnostics.Debug.WriteLine(category.Name + ": " + category.ContentLink);
                categoryRefrences.Add(category.ContentLink);
            }

            return categoryRefrences;
        }

private void RemoveCategories(string productCode, IList<ContentReference> categoryLinks)
        {
            // cleans up all the existing categories
            string methodName = MethodBase.GetCurrentMethod().Name;
            var productContentLink = this.referenceConverter.GetContentLink(productCode, CatalogContentType.CatalogEntry);

            foreach (var categoryLink in categoryLinks)
            {
                var relationToRemove = new NodeRelation
                {
                    Child = productContentLink,
                    Parent = categoryLink,
                };

                // Removes matching NodeRelation, or no action if no match exists
                this.relationRepository.RemoveRelation(relationToRemove);

                var temp = this.contentLoader.Get<CategoryNode>(categoryLink);
                string message = string.Format(CultureInfo.CurrentCulture, "Line {0}: Category '{1}' for product '{2}' REMOVED.", this.recordCount, productCode, temp.Name);
                logger.LogInformation(message, methodName);
            }
            
      
        }
#255116
Edited, May 19, 2021 16:40
Vote:
 

Here is the pseudo code:

var relations = relationRepository.GetParents<NodeEntryRelation>(productLink).ToList();

foreach (var categoryLink in categoryLinks)

{

 var relationToRemove = relations.FirstOrDefault(x=>x.Parent == categoryLink);

if (relationToRemove != null)

relations.Remove(relationToRemove);

}

relationRepository.UpdateRelations(relations);

#255173
May 20, 2021 7:48
Vote:
 

Thanks @Quan Mai for taking time out and answering.

I am not abel to reach remove association.

The problem is is "categoryLinks". They always come 0.

To remove association, I need to have categoryContentReference but that is not returned.

var productContentLink = this.referenceConverter.GetContentLink(productCode, CatalogContentType.CatalogEntry);
            IList<ContentReference> categoryRefrences = new List<ContentReference>();

            var categories = this.contentLoader.GetChildren<CategoryNode>(productContentLink);

            // THE ABOVE ALWAYS HAVE COUNT 0
#255181
May 20, 2021 9:32
Vote:
 

The problematic line is this  var categories = this.contentLoader.GetChildren<CategoryNode>(productContentLink);

You can't get children of a product. If you want to replace  the existing parents of a product, just do this

var relations = new List<NodeEntryRelation>();

//add relations from your CSV to relations

relationRepository.UpdateRelations(relations);

#255216
May 20, 2021 15:53
Vote:
 

Hi Shaan,

Try the below code and replaced 'FashionProduct' with '[Your Product Type]' for retrieving the product categories.

      private IList<ContentReference> GetCategories(string productCode)
        {
            var contentLink = _referenceConverter.GetContentLink(productCode, CatalogContentType.CatalogEntry);
            var productContentLink = _contentLoader.Get<FashionProduct>(contentLink);

            var contentBase = productContentLink as EntryContentBase;
            var categories = this.GetCategoriesAll(contentBase).ToList();

            IList<ContentReference> categoryReferences = new List<ContentReference>();
            foreach (var category in categories)
            {
                System.Diagnostics.Debug.WriteLine(category.Name + ": " + category.ContentLink);
                categoryReferences.Add(category.ContentLink);
            }

            return categoryReferences;
        }

        private IEnumerable<NodeContent> GetCategoriesAll(EntryContentBase child)
        {
            var list = new List<NodeContent>();

            if (child != null)
            {
                var parentLinks = child.GetCategories(_relationRepository);

                foreach (var parentLink in parentLinks)
                {
                    if (!_contentLoader.TryGet(parentLink, out NodeContent parent))
                        continue;

                    list.Add(parent);
                    list.AddRange(this.GetCategoriesAll(parent));
                }
            }

            return list;
        }

        private IEnumerable<NodeContent> GetCategoriesAll(NodeContent child)
        {
            var list = new List<NodeContent>();

            if (child != null)
            {
                var parentLinks = child.GetCategories(_relationRepository);

                foreach (var parentLink in parentLinks)
                {
                    if (!_contentLoader.TryGet(parentLink, out NodeContent parent))
                        continue;

                    list.Add(parent);
                    list.AddRange(this.GetCategoriesAll(parent));
                }
            }

            return list;
        }
#255225
May 20, 2021 18:29
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.