Developer forum

Forum » Development » Displaying Multiple Products with Variants on a Single Page

Displaying Multiple Products with Variants on a Single Page


Reply
Hi there,

I am building a custom module that needs to show multiple products on the same page. For each product, I need to be able to do the following:

1. Display up to three related drop-down list for variants. E.g. Size, Color and Type. These drop-downs should be filtered / filled based on the previous options when they are available as a variant.

2. Allow multiple products to be selected and added to the cart. Each product can be part of a set, so users need to be able to select one ore more products and add them at once.

With craploads of server side code, API calls and a rediculous amount of JavaScript I can more or less make this work for a single product although it's pretty shaky and ugly. However, I run into problems when I have multipe products. One of the reasons for this problem is that inside my nested loop that writes out the variants, I can't access the loop counter defined in the outer loop.

For example, consider this:

<!--@LoopStart(Products)-->
  var myList<!--@Products.LoopCounter--> = new VariantSelector();<!--@LoopStart(VariantGroups)--> 
  group = new VariantGroup("<!--@Ecom:VariantGroup.ID-->","<!--@Ecom:VariantGroup.Name-->","<!--@Ecom:VariantGroup.Label-->");
  <!--@LoopStart(VariantOptions)-->group.Options.push(new VariantOption("<!--@Ecom:VariantOption.ID-->","<!--@Ecom:VariantOption.Name-->"));
  <!--@LoopEnd(VariantOptions)-->myList<!--@Products.LoopCounter-->.Groups.push(group);
<!--@LoopEnd(VariantGroups)-->
<!--@LoopStart(VariantCombinations)-->  myList<!--@Products.LoopCounter-->.Combinations.push(new VariantCombination("<!--@Ecom:VariantCombination.VariantID-->"));
<!--@LoopEnd(VariantCombinations)-->
<!--@LoopEnd(Products)-->

In the second call to myList (to fill a JavaScript variable defined in the outer loop), I don't get access to the @Products.LoopCounter template tag which I need to successfully declare the myList outside the loop and use it inside one, and repeat that multiple times. E.g.:

var myList1;
myList1.Groups.push(....)
myList1.Groups.push(....)
myList1.Groups.push(....)

var myList2;
myList2.Groups.push(....)
myList2.Groups.push(....)
myList2.Groups.push(....)

Can someone show me how to do this? Is there any source code available from demo projects that show this is done?

Cheers,

Imar


Replies

 
Reply
Hi Imar

I agree that it is counter-intuitive that you can not access loop variables inside nested loops, considering loops in a programming language.

However, I think your problem can be solved in this way:

Instead of naming your lists myList1, myList2, ... you declare an array of these lists:

var myLists = new Array();
<!--@LoopStart(Products)-->
    var myList = new VariantSelector();
    myLists.push(myList);

    <inside nested loop>
       myList.Groups.push(...);
    </inside loop>

<!--@LoopEnd(Products)-->

In the end you will have an array of lists, where the index coresponds to the loopcounter of products.

 - Lasse
 
Reply
Hi Lasse,

Thanks for that; I'll give it a try. Makes sense as a work around...

Imar

 

You must be logged in to post in the forum