Developer forum

Forum » Ecommerce - Standard features » Products with two variants (size and color)

Products with two variants (size and color)

Jakob Westhausen
Reply

Hey guys

 

I'm using the new index on a solution and on the product list each color variant is rendered by using the "color - First variant in group" expression, which works fine. But I want to show which sizes is availible on each color variant, both on the product list and the product view. However both the VariantCombinations, VariantGroups and VariantAvailableOptions dosen't contain the relation between the size, color and stock count.

I really hope you guys can help me..

 

I have attached a screenshot of what I want to achieve. Inspired by this site: http://www.levi.com/DK/da_DK/mens-jeans/p/055100597

 

BR

 

Jakob Westhausen

variants.jpg

Replies

 
Nicolai Pedersen
Reply

There is also a VariantStockCombinations loop. Could you take a look at that?

 
Jakob Westhausen
Reply

That loop dosen't contain information about the variant name or the Ecom:VariantOption.ImgSmall.Clean which is use to display a hex code/thumbnail. I think it only renders the size options, which still dosen't have a relation to the color.

I have fixed it, but the code requires a bit too many loops..

 

 

  // colorList out {id,name,color value,selected}
  List colorList = new List();
  // sizeList out {id,name,stock count, available (related to selected color), selected}
  List sizeList = new List();

  // Reconstruct the variant data
  string productVariantId = GetString("Ecom:Product.VariantID").Length > 0 ? GetString("Ecom:Product.VariantID").Split('.')[1] : null;

  foreach( var item in GetLoop("VariantCombinations"))
  {
    string variantId = item.GetString("Ecom:VariantCombination.VariantID");
    string variantStock = item.GetString("Ecom:VariantCombination.Product.Stock");
    string variantSelected = item.GetString("Ecom:VariantCombination.Selected");

    foreach( LoopItem subitem in item.GetLoop("VariantGroups").Where( subitem => subitem.GetString("Ecom:VariantGroup.ID") == "FARVE" ).SelectMany( subitem => subitem.GetLoop("VariantAvailableOptions") ).Where( subitem => subitem.GetString("Ecom:VariantOption.ID") == variantId.Split('.')[1] ) )
    {
      string variantName = subitem.GetString("Ecom:VariantOption.Name");
      string colorValue = !string.IsNullOrWhiteSpace( subitem.GetString("Ecom:VariantOption.ImgSmall.Clean") ) ? subitem.GetString("Ecom:VariantOption.ImgSmall.Clean") : variantName;
      string variantColorSelected = variantId.Split('.')[1] == productVariantId ? "true" : "false";

      if( !colorList.Any(c => c.Contains( colorValue ) ) )
      {
        colorList.Add( variantId + "," + variantName + "," + colorValue +  "," +  variantColorSelected);
      }
    }
    foreach( LoopItem subitem in item.GetLoop("VariantGroups").Where( subitem => subitem.GetString("Ecom:VariantGroup.ID") == "STR" ).SelectMany( subitem => subitem.GetLoop("VariantAvailableOptions") ).Where( subitem => subitem.GetString("Ecom:VariantOption.ID") == variantId.Split('.')[0] ) )
    {
      string sizeAvailable = variantId.Split('.')[1] == productVariantId ? "true" : "false";
      string variantName = subitem.GetString("Ecom:VariantOption.Name");
      sizeList.Add( variantId + "," + variantName + "," + variantStock +  "," + sizeAvailable + "," + variantSelected );
    }
  }
 
Nicolai Pedersen
Reply

Hi Jakob

Thanks for sharing. I can see the issue if this is the case.

I'll have a look at this and see what improvements we can make to those loops to get you the required information.

BR Nicolai

 

You must be logged in to post in the forum