You are viewing an old revision of this post, from November 24, 2015 @ 11:51:53. See below for differences between this version and the current revision.

Getting selected simple product id in configurable product on client side.

If you need to get id of selected simple product in configurable product on client side you can do it in many different ways. Here is simple function how to achieve that with no code modification, new templates or even modules. Just one Javascript file and layout update.
Product.Config.prototype.getIdOfSelectedProduct = function()
{
     var existingProducts = new Object();
 
     for(var i=this.settings.length-1;i>=0;i--)
     {
         var selected = this.settings[i].options[this.settings[i].selectedIndex];
         if(selected.config)
         {
         	for(var iproducts=0;iproducts<selected.config.products.length;iproducts++)
         	{
         		var usedAsKey = selected.config.products[iproducts]+"";
         		if(existingProducts[usedAsKey]==undefined)
         		{
         			existingProducts[usedAsKey]=1;
         		}
         		else
         		{
         			existingProducts[usedAsKey]=existingProducts[usedAsKey]+1;
         		}
             }
         }
     }
 
     for (var keyValue in existingProducts)
     {
     	for ( var keyValueInner in existingProducts)
         {
         	if(Number(existingProducts[keyValueInner])<Number(existingProducts[keyValue]))
         	{
         		delete existingProducts[keyValueInner];
         	}
         }
     }
 
     var sizeOfExistingProducts=0;
     var currentSimpleProductId = "";
     for ( var keyValue in existingProducts)
     {
     	currentSimpleProductId = keyValue;
     	sizeOfExistingProducts=sizeOfExistingProducts+1
     }
 
     if(sizeOfExistingProducts==1)
     {
    	 alert("Selected product is: "+currentSimpleProductId)
     }
}

Revisions

  • November 24, 2015 @ 11:51:53 [Current Revision] by admin
  • November 24, 2015 @ 11:51:53 by admin

Revision Differences

There are no differences between the November 24, 2015 @ 11:51:53 revision and the current revision. (Maybe only post meta information was changed.)

No comments yet.

Leave a Reply