Developer forum

Forum » Ecommerce - Standard features » Variant Options for a Product not working in a cshtml template

Variant Options for a Product not working in a cshtml template

Kevin O''''Driscoll
Reply
Why does this not work?
Im using the same data for the same product in the same dw 8.9.1.7 ecommerce solution, I have a product.html template and a product.cshtml template
with the following code:
 
html version works
<!--@LoopStart(VariantGroups)-->
        <!--@Ecom:VariantGroup.Name-->
        <select name="<!--@Ecom:VariantGroup.ID-->">
        <!--@LoopStart(VariantAvailableOptions)-->
           <!--We get some nice Variant Options-->
          <option value="<!--@Ecom:VariantOption.ID-->" <!--@If Defined(Ecom:VariantOption.Selected)-->selected="selected"<!--@EndIf(Ecom:VariantOption.Selected)-->><!--@Ecom:VariantOption.Name--></option>
          <!--@LoopEnd(VariantAvailableOptions)-->
        </select><br />
<!--@LoopEnd(VariantGroups)-->
 
 
cshtml version does not work
@foreach (LoopItem i in GetLoop("VariantGroups"))
        {
           @i.GetValue("Ecom:VariantGroup.Name")
            <select name="@i.GetValue("Ecom:VariantGroup.ID")">
               @*Sadly, no Variant Options*@
        @foreach (LoopItem i2 in GetLoop("VariantAvailableOptions")){
          <option value="@i2.GetString("Ecom:VariantOption.ID")" @if(Dynamicweb.Input.FormatBoolean(i2.GetString("Ecom:VariantOption.Selected"))){<text>selected="selected"</text>}>@i2.GetString("Ecom:VariantGroups.VariantOption.Name")</option>
                     }
 
            </select><br />
}
 
What am I doing wrong?

Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

You need to call GetLoop on the i variable you're iterating over:

 @foreach (LoopItem i2 in i.GetLoop("VariantAvailableOptions")){

I would recommend using something other than i though and use something like variantGroup. Makes the code easier to read and issues like this easier to spot:

@foreach (var variantGroup in GetLoop("VariantGroups"))
{
  @variantGroup.GetValue("Ecom:VariantGroup.Name")
  <select name="@variantGroup.GetValue("Ecom:VariantGroup.ID")">
    @foreach (var availableOption in variantGroup.GetLoop("VariantAvailableOptions")){
      <option value="@availableOption.GetString("Ecom:VariantOption.ID")" @if(Dynamicweb.Input.FormatBoolean(availableOption.GetString("Ecom:VariantOption.Selected"))){<text>selected="selected"</text>}>@availableOption.GetString("Ecom:VariantGroups.VariantOption.Name")</option>
    }
  </select>
  <br />
}

Cheers,

Imar

Votes for this answer: 1
 
Kevin O''''Driscoll
Reply

Thanks Imar well spotted, I learn not to trust completely the html/cshtml converter...

@availableOption.GetString("Ecom:VariantGroups.VariantOption.Name")

should be

@availableOption.GetString("Ecom:VariantOption.Name")

Cheers Kevin

 

You must be logged in to post in the forum