Developer forum

Forum » Development » Swift show new attribute instead of product number

Swift show new attribute instead of product number

Caro De Weze
Reply

Hi,

I want to show 'Art. Ref.' instead of 'Number' for my products at the frontend, so attribute Art_ref instead of Product_Number. How do I do this by adjusting for example the Swift_ProductNumber.cshtml template?

I tried the following approaches to access the Art_ref field, but none have worked:

  • Direct Property Access: Tried @product.Art_ref, but it resulted in an error as Art_ref is not a direct property of ProductViewModel.
  • Tags and GetString: Attempted @GetString("Ecom:Product.Field.Art_ref") and @Html.Raw(@GetString("Ecom:Product.Field.Art_ref")), but these methods and tags were not available in the current context.
  • Access via ProductFields Dictionary: Tried retrieving Art_ref using @product.ProductFields["Art_ref"].Value as string, but the field was either not found or not accessible. 

Could you advise on how to properly access the Art_ref field in this context? Afterwards I have to replace it everywhere, but I want to start at the detail page of a product.

Kind regards,
Caro De Weze


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

In that template, you can make a change like this:

 

And the code:

string productNumber = product.Number;
if (product.ProductFields.TryGetValue("Art_ref", out FieldValueViewModel? myProductNumber) 
 && !string.IsNullOrEmpty(Dynamicweb.Core.Converter.ToString(myProductNumber.Value))){
 productNumber = Dynamicweb.Core.Converter.ToString(myProductNumber.Value);
}

<div class="fs-7 opacity-85 @horizontalAlign item_@Model.Item.SystemName.ToLower()" itemprop="sku">@productNumber </div>
 
Caro De Weze
Reply

Hi Nicolai,

Thanks for the quick response.

Unfortunately the number is still shown and not the ref. Can you please look again?

Kind regards,
Caro De Weze

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

It looks right to me. Since I do not have your setup, I cannot test it further. I think you have to fiddle with the template to make it work.

Ensure you use the right system name with the right casing - Art_ref might not be 100% correct.

 
Caro De Weze
Reply

Hi Nicolai,

The system name is correct:

Is the retrieval of the data different because it is a global custom field?

Kind regards,
Caro De Weze

 

You must be logged in to post in the forum