Posted on 15/05/2020 07:12:43
Hello Aki Ruuskanen
1) In Ecom we render product category fields in the natural order, I mean in that order when fields was added(and shown) in Setting >> Ecommerce >> Product catalog >> Product categories >> <Edit Category>
2) In PIM you set up order product category fields by yourself using "Visible Fields". And this is user specific. So each user can set up order by himself.
3) It is not simple to recognize how you're rendering category fields in frontend using ProductFieldService becase we haven't service with such name.
So below I want to show two ways to render fields which you can use:
a) Render category fields in Ecom order:
Just use Services.ProductCategoryFields.GetFieldsByCategoryId to get category fields and transform it to ProductField. Similar like these lines:
private IEnumerable<ProductField> GetCategoryFields(string categoryId)
{
var categoriesFieldsIdx = ProductField.GetCategoryFields().ToDictionary(f => f.SystemName);
var category = Dynamicweb.Ecommerce.Services.ProductCategories.GetCategoryById(categoryId);
if (category == null)
{
throw new ArgumentException($"The category {categoryId} doesn't exist");
}
var fields = category.Fields;
var result = new List<ProductField>();
foreach(var field in fields)
{
var sysName = $"ProductCategory|{field.Category.Id}|{field.Id}";
ProductField prodField;
if (categoriesFieldsIdx.TryGetValue(sysName, out prodField))
{
result.Add(prodField);
}
}
return result;
}
Look to whole example in CategoryFieldsRenderer.cs in Gist1
b) Use the 'Field display group' to render fields:
So go to Setting >> Ecommerce >> Product catalog >> Field display groups and create display group with needed fields.
You need find Id this display group: Go to "Edit" this group and Id will in URL(yes it's alitle bit tricky we already added systemname for it. The system name will be delivered in the 9.9 only).
So after use can get display group ProductFields using:
private IEnumerable<ProductField> GetDisplayGroupFields(int displayGroupId)
{
var displayGroup = Dynamicweb.Ecommerce.Services.FieldDisplayGroups.GetById(displayGroupId);
if (displayGroup == null)
{
throw new ArgumentException($"The display group {displayGroupId} doesn't exist");
}
var fieldsUniqueSystemNames = Core.Converter.ToString(displayGroup.FieldIds).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var allFields = ProductField.GetAllEditableProductFields();
var result = new List<ProductField>();
foreach (var fieldSystemName in fieldsUniqueSystemNames)
{
ProductField prodField;
if (allFields.TryGetValue(fieldSystemName, out prodField))
{
result.Add(prodField);
}
}
return result;
}
Look to whole example in FieldsDisplayGroupRenderer.cs in Gist2
I hope I help you.
P.S.
And also in the product catalog frontend pages you can use field display groups as tags just look into FieldDisplayGroups tag loop