tags:

views:

489

answers:

2

Hey guys,

I have the following code to grab a list of Products

$collection = Mage::getModel('catalog/product')->getCollection();

$collection->addAttributeToSelect('name')
     ->addAttributeToFilter("category_ids", array('finset'=>$this->category_id));

foreach($collection as $product) {
   echo $product->getName();
}

My question is, how can I NOT echo products that are 'simple' but belong to a parent 'configurable' product. (for example don't show "Red Shirt Medium" as it belongs to "Red Shirt")

I have worked out that this association lives in 'catalog_product_super_link' but I have only just started with Magento and unfortuantely don't know how to do the filtering :)

Cheers guys,

Chris.

+2  A: 

I don't know a direct way to add this condition to the collection, I'd be interested in such a solution too. But you can always check inside the loop for each product:

if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()))
    echo $product->getName();
michaelk
+1  A: 

Theres a function called isConfigurable in the product class.

That may be of help for you.

$product->isConfigurable(); // if its the parent object it'll be true, if its the child itll be false.

John Cuthbert