Skip to content

Instantly share code, notes, and snippets.

@bomber-lomber
Last active May 15, 2020 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bomber-lomber/5f149980adb78113b4f98607b2aa13f0 to your computer and use it in GitHub Desktop.
Save bomber-lomber/5f149980adb78113b4f98607b2aa13f0 to your computer and use it in GitHub Desktop.
FieldsDisplayGroupRenderer
using Dynamicweb.Ecommerce.Products;
using System;
using System.Collections.Generic;
namespace Dynamicweb.Education.Ecommerce
{
public class FieldsDisplayGroupRenderer
{
public string Render(ProductInfo[] products, int displayGroupId)
{
var fields = GetDisplayGroupFields(displayGroupId);
var result = "<table class=\"table\"><thead><tr>";
foreach (var field in fields)
{
result += $"<th>{field.Name}</th>";
}
result += "</tr></thead>";
result += "<tbody>";
foreach (var productInfo in products)
{
var product = Dynamicweb.Ecommerce.Services.Products.GetProductById(productInfo.ProductId, productInfo.VariantId, productInfo.LanguageId);
if (product == null)
{
continue;
}
result += "<tr>";
foreach (var field in fields)
{
var propValue = Dynamicweb.Ecommerce.Services.Products.GetFieldValue(product, field);
result += $"<td>{(propValue.Succeeded && propValue.Data != null ? propValue.Data : string.Empty)}</td>";
}
result += "</tr>";
}
result += "</tbody></table>";
return result;
}
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;
}
}
}
namespace Dynamicweb.Education.Ecommerce
{
public class ProductInfo
{
public string ProductId { get; set; }
public string LanguageId { get; set; }
public string VariantId { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment