7

In product details page when user choose like color and size it returns simple product id of thatenter image description here

5

I have select box at attribute of configurable product. This is what i have done to get simple product id. Put following code in your configurable product phtml file.

 require(["jquery"], function(jQuery) {
      jQuery(".product-options-wrapper select[id^='attribute']").last().on('change', function() {
          setTimeout(function (){
            simpleId=jQuery("input[name=selected_configurable_option]").val();
            alert(simpleId);
          }, 500); 
      });
}); // Required

If you have configurable swatch than it will required minor change in above code.Hope it will help someone.

Thank you

1

Try below code.

foreach($collection as $_product) {
    $_children = $_product->getTypeInstance()->getUsedProductIds($_product);
    $getChildId = array();
    foreach ($_children as $child){
        $getChildId[] = $child->getID();
    }
    print_r($getChildId);
}
  • thanks for replay but i want somthing more,when i click size of any value with respective color it return only that simple product id . – Jitendra Mohanta Sep 9 '16 at 10:24
1

If you need to get id of selected simple product in configurable product on client side you can do it by below way.

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)
     }
}
  • You should update your current answer. – Khoa TruongDinh Sep 9 '16 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.