Developer forum

Forum » Templates » Show Product field name instead of value

Show Product field name instead of value

Alex Guo
Reply

Hi everyone,

Is there a way to display the Name instead of the value of a ProductField?

I can only find GetProductFieldValue(Product, string) as function in the documentation

Thanks in advance,
Alex


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Alex

Yes, you can do that. I believe you are in a tempalte and try to show the name of the selected options instead of the value?

Example 1 - iterating all product categories of a product

@inherits ViewModelTemplate<ProductViewModel>
@using Dynamicweb.Rendering
@using Dynamicweb.Ecommerce.ProductCatalog

@{
 //Iterate through the field display groups and their fields
 foreach (var displayGroup in Model.FieldDisplayGroups)
 {
 <h3 id="@displayGroup.Value.Id">@displayGroup.Value.Name</h3>

 //Iterate the fields of the display group
 foreach (var field in displayGroup.Value.Fields)
 {
 <b>@field.Value.Name:</b>

 //Check if the field is a list of options and display the name of the selected options and not the value
 if (field.Value.Value is List<FieldOptionValueViewModel> listValues)
 {

 //Iterate each selected option and display the name with the value in a html attribute
 foreach (var fieldOptionValue in listValues)
 {
 <div data-field-value="@fieldOptionValue.Value">@fieldOptionValue.Name</div>
 }
 }
 else
 {
 //otherwise display the value
 <div>@field.Value.Value</div>
 }
 }
 }
}

Example 2 - iterating selected display groups of a product

@{
 //Iterate through the field display groups and their fields
 foreach (var displayGroup in Model.GetProductDisplayGroupFieldsByGroupSystemNames(["someDisplayGroup"]))
 {

 <h3 id="@displayGroup.SystemName">@displayGroup.Name</h3>

 //Iterate the fields of the display group
 foreach (var field in displayGroup.Fields)
 {
 <b>@field.Name:</b>

 //Check if the field is a list of options and display the name of the selected options and not the value
 if (field.Value is List<FieldOptionValueViewModel> listValues)
 {
 //Iterate each selected option and display the name with the value in a html attribute
 foreach (var fieldOptionValue in listValues)
 {
 <div data-field-value="@fieldOptionValue.Value">@fieldOptionValue.Name</div>
 }
 }
 else
 {
 //otherwise display the value
 <div>@field.Value</div>
 }
 }
 }
}
 
Alex Guo
Reply

Hey,

I managed to display the value using the FieldId like this:

public string GetUnitName(string productNumber)
{
            var fieldSystemName = "Unit";
var productObject = Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productNumber, Dynamicweb.Ecommerce.Common.Context.LanguageID);

if (productObject == null)
{
return "";
}

var productField = ProductField.GetById("FIELD32");
var fieldValue = Dynamicweb.Ecommerce.Services.Products.GetProductFieldValue(productObject, fieldSystemName) as string;
var fieldOption = productField.GetFieldOptions()?.FirstOrDefault(option => option.Value.Equals(fieldValue, StringComparison.OrdinalIgnoreCase));
var optionName = fieldOption.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID);

return !string.IsNullOrEmpty(optionName) ? optionName : "";
}

Now I’m trying to display other custom global fields for another project,

but my previous solution doesn’t work in this case, so I tried your approach instead:

 public string GetCustomFieldValue(string productNumber, string fieldSystemName, string productFieldId)
    {
        var productObject = Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productNumber, Dynamicweb.Ecommerce.Common.Context.LanguageID);
   
        if (productObject == null)
        {
            return "";
        }
        
        
        object fieldValue = Dynamicweb.Ecommerce.Services.Products.GetProductFieldValue(productObject, fieldSystemName);
            
        if (fieldValue == null)
        {
            return "";
        }
            
        return fieldValue.ToString() ?? "";

   }

    public string GetUnitsBox(string productNumber)
    {
        return GetCustomFieldValue(productNumber, "UnitsBox", "FIELD133");
    }

    public string GetUnitsPallet(string productNumber)
    {
        return GetCustomFieldValue(productNumber, "UnitsPallet", "FIELD134");
    }

However, this always returns 0.

They do have higher values in the backend though.

Any idea why this might be happening?
Am I using this wrong?

Thanks in advance for the help!

 

You must be logged in to post in the forum