26

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.

Arun Karnawat
  • 575
  • 10
  • 21
chris.rickard
  • 1,035
  • 1
  • 11
  • 11

5 Answers5

31

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();
}
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
michaelk
  • 739
  • 5
  • 9
  • 1
    Perfect solution for finding parent ids of a simple. Thanks very much – Neil Aitken Dec 02 '11 at 12:29
  • 2
    _"I don't know a direct way to add this condition to the collection"_ `$collection->getSelect()` and we are able to use [Zend_Db_Select](http://framework.zend.com/manual/1.12/en/zend.db.select.html) to build custom select. A `$collection->getTable('catalog/product_super_link');` can be helpful for joins. – Marcin Rogacki Feb 16 '14 at 15:24
6

I've done something similar for our google feed. This excerpt of code is what I use to check the products inheritance:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('price', array('gt' => 0) );//price not 0
//$products->addAttributeToFilter('visibility', 4); //catalog, search - comment out to show all items (configurable products simple product breakdowns)

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);

$prodIds=$products->getAllIds();
try {
foreach($prodIds as $productId) {
    $product = Mage::getModel('catalog/product');
    $product->load($productId);

    // SIMPLE PRODUCTS
    if($product->getTypeId() == 'simple' ) {
        $prodName = trim($product->getName());

        $parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);
        if(!$parentIds)
            $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);

        if($parentIds) {    
            $parentProd = Mage::getModel('catalog/product')->load($parentIds[0]);           
            /* 
             * do something if this product has a parent or do some checks against $parentProd
             */
         } // end parent check  
    }//if SIMPLE
} // foreach

} catch(Exception $e) {
    die($e->getMessage());
}
deepmark
  • 93
  • 2
  • 5
  • $product->getTypeId() is what I needed. Options are "simple" and "configurable" – Ian Jun 13 '19 at 10:01
5

There's a function called isConfigurable in the product class.

That may be of help to you.

$product->isConfigurable(); 
// if its the parent object it'll be true, if its the child it'll be false. 
gmadd
  • 1,146
  • 9
  • 18
John Cuthbert
  • 186
  • 2
  • 5
  • 7
    I think this will not work, if the product is a simple product, this will always return false, but the product can be/not-be a child. – JuLy Apr 02 '13 at 11:08
2

The quickest way might be to check if the product's visibility is set to "Not Visible Individually", since simple products associated with configurable products are typically set to this. Unfortunately I don't know the precise syntax but hopefully someone else who's willing to chime in does!

cfx
  • 3,311
  • 2
  • 35
  • 45
1

Since simple products that are part of configurable products usually have a visibility value of Not Visible Individually, it probably suffices to add a visibility filter to the collection that checks for visibility of the products in the catalog:

$collection->setVisibility(Mage::getModel('catalog/product_visibility')->getVisibleInCatalogIds());

In the unlikely circumstance that the resulting products are part of a configurable product, you can use the method Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChild to check if a product is used as part of a configurable product.

Aad Mathijssen
  • 630
  • 9
  • 22