Saturday, 29 December 2012

Magento get Associated / Child products in configurable products

Magento get Associated / Child products in configurable products

I.)  Configurable Products contains set of Child products/Associated products. The following code can control  Child products/Associated products. We have to open configurable.php in the following path.
    
magento\magento\app\design\frontend\your-template-foler\default\template\catalog\product\view\type\options
configurable.php

Program:-

<?php
$_product    = $this->getProduct(); 
if($_product->isConfigurable())
{
      $_associatedProducts = $_product->getTypeInstance()->getUsedProducts();
       foreach($_associatedProducts as $assProducts)
      {

            echo $assProducts->getId();

            echo $assProducts->getSku();
            $_gallery = Mage::getModel('catalog/product')->
                                load($assProducts->getId())->getMediaGalleryImages();

            foreach ($_gallery as $_image )
           { ?>
                    <img src="<?php echo $this->helper('catalog/image')-> init($assProducts,'thumbnail',$_image->getFile())->resize(350); ?>" />

<?php  }

       }
}
?>

Discription : -
         1.  $_product    = $this->getProduct(); -> Get Current Product and store with $_product variable.
         2. $_product->isConfigurable();   ->   Condition for if Configurable product or not.
         3.  $_associatedProducts = $_product->getTypeInstance()->getUsedProducts(); ->  Get Current product's child products.


Program :-


<?php
foreach($_associatedProducts as $assProducts)
{
        $sku = $assProducts->getSku();
        $product = Mage::getModel('catalog/product');
        $productId = $product->getIdBySku( $sku );
        $product->load($productId);
     
        foreach ($product->getOptions() as $o)
        {
         }
        $values = $o->getValues();
        ?>
       
        <select id="<?php echo $assProducts->getId(); ?>" >
        <?php
        echo "<option>".'Choose a Size'."</option>";
        foreach ($values as $v)
        {
        ?>      
                <option value='<?php echo $v->getData('option_type_id'); ?>'><?php echo $v->getData('title'); ?> </option>
        <?php
        }
       
        ?>
        </select>
        <?

 
}            
?>

No comments:

Post a Comment