Nathaniel Wendt
Nathaniel Wendt

Reputation: 1202

Magento Get Child Product Variation

I feel like the flexibility of Magento would allow for my problem to be fixed but I haven't found anything as of yet.

So essentially I need to get the attribute value for a child product of a configurable product. So far all I can do is in the view.phtml file:

if ($_product->getTypeId() == 'configurable')
{
    $confAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
    print_r($confAttributes);
}

But that is from the parent scope of things. Basically my problem is that I need to get the images of the child products but when I go through a loop like this...

if ($_product->getTypeId() == 'configurable')
  $_child_products = $_configurable_model->getUsedProducts(null, $_product);
for ($i = 0; $i < count ($_child_products); $i++){
    <?php echo $this->helper('catalog/image')->init($_child_products[$i], 'image'); ?>
}

But now that is the scope from the perspective of the child product. I need to somehow correlate the child product with the attribute value that it takes on (for use with jQuery and image manipulation).

So is there any way that I can get some information from the child_product's perspective that can link it to the attribute?

Upvotes: 2

Views: 4463

Answers (1)

Nazneen
Nazneen

Reputation: 789

This might help you:

    $product = Mage::getModel('catalog/product')->load($id);
    
    $childIds = Mage::getModel('catalog/product_type_grouped')
    ->getChildrenIds($product->getId());
    
    $i = 1;
    
    foreach ($childIds as $key => $val){
        
        foreach($vals as $keyy => $vall){
            $arr[$i] = $vall;
            $i++;
        }
        
    }

$id -> is the product id of the group product.

$arr -> array which contains the id's of child products.

        $collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('price')
    ->addFieldToFilter('entity_id',array('in' =>array($arr)));

above code snippet shows how the way to retrieve the child products.

Upvotes: 2

Related Questions