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);
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
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);
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;
}
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.