Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design
Showing posts with label simple product id. Show all posts
Showing posts with label simple product id. Show all posts

Get Simple product details of configurable products like ID, SKU, Product Name

If you want to get Simple product details of configurable product like simple product Id, sku, product name etc. you can get using my simple module.

Magento Simple product details of configurable products

Before using this module you need to add simple script for getting simple product id from configurable product. Check my previous post Get Simple product of configurable product.

In that script you just need to change following ajax code instead of alert box.
Note: You must need to add jQuery library in your page if library is not already added before using ajax code.

jQuery.ajax({
    type: "POST",
    url: "<?php echo $this->getBaseUrl()?>productdetails/index/optiondetails/",
    data:"id="+currentSimpleProductId,
    success: function(msg)
    {
        alert(msg);
        //var data = JSON.parse(msg);
        //alert(data.id);
    }
});

Download Source

Magento get selected simple product id of configurable product on Product details page


Using my this solution you can get simple product Id of configurable product on product details page.

Here is simple function how to achieve that with no code modification.
Just one Javascript file and layout update.

configurable product


How are we going to pre-select configurable attributes? The selection of the attributes is controlled by the spConfig Javascript object. If you supply it with propertydefaultValues then the script will automatically select respective items in the option drop-down lists.

Paste the following code in app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml in side the script

spConfig.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)
}

}

Now add onchange event to your dropdown in same page :

onchange="spConfig.getIdOfSelectedProduct()"

your dropdown's html looks like this...


<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select" onchange="spConfig.getIdOfSelectedProduct()">
        <option><?php echo $this->__('Choose an Option...') ?></option>
 </select>

Now you will get simple product id of your selected configurable product in alert box. So now you can get any details of your simple product using this product id.

Update: Click here if you want to download my module for getting all product details of selected simple product.


 

Copyright @ 2017 HKBlog.