Last active
May 15, 2020 04:51
-
-
Save bomber-lomber/5f149980adb78113b4f98607b2aa13f0 to your computer and use it in GitHub Desktop.
FieldsDisplayGroupRenderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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