Posted on 13/06/2014 12:58:33
Hi Mikkel,
Something like this would do the trick:
using System.Collections.Generic;
using System.Linq;
using Dynamicweb.eCommerce.Products.Categories;
using Dynamicweb.Extensibility;
using Dynamicweb.Rendering;
namespace Your.Project
{
[AddInName("RenderProductFieldLabels")]
public class RenderProductFieldLabels : TagExtensionMethod
{
public override string ExecuteMethod(string value)
{
// Get the ProductCategory
string productCategoryName = Arguments[0].ToString();
var productCategory = Category.GetCategoryByID(productCategoryName);
if (productCategory == null) return value;
// Get the FieldName
string productCategoryFieldName = Arguments[1].ToString();
var productFieldName = productCategory.Fields.FirstOrDefault(x => x.ID == productCategoryFieldName);
if (productFieldName == null) return value;
// Get the FieldOptions
var availableOptions = productFieldName.FieldOptions;
// Determine the separator that is placed between the items
string separator = Arguments[2].ToString();
// Get the selected options for this product
var selectedOptions = new List<string>(value.Split(','));
// Find the options in the available options that are selected and then grab their Name (which is the Label in the backend)
var optionsList = availableOptions.Where(x => selectedOptions.Contains(x.Value)).Select(x => x.Name);
return string.Join(separator, optionsList.ToArray());
}
}
}
This code expected three parameters: the system name of the product category, the system name of the individual field, and the separator that will be placed between items if you have a multi-select like a check box list. You can call it as follows:
<!--@Ecom:Product.CategoryField.CategorySystemName.FieldSystemName.Value.RenderProductFieldLabels("CategorySystemName", "FieldSystemName", "<br />")-->
This puts each item on a separate line. If you pass a comma you get a comma separated list. You can modify the code to render wrapping tags too, like an <li>
Hope this helps,
Imar