0

How to get a collection of all configurable products and simple products ( not belonging to configurable product) in Magento custom module?

I have a custom code for getting all simple product which does belong to configurable product:

$collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'))
    ->addAttributeToSelect('*'); //or just the attributes you need

$collection->getSelect()->joinLeft(array('link_table' => 'catalog_product_super_link'),
    'link_table.product_id = e.entity_id',
    array('product_id')
);
$collection->getSelect()->where('link_table.product_id IS NULL');

And also have code for getting all configurable products:

$result = array();
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'))->addAttributeToSelect('*');
foreach($collectionConfigurable as $configurProduct)
{
    $Title=$configurProduct->getName();
    $id=$configurProduct->getId();
    $result[] = array('value'=>$id,'label'=>$Title);
}
return $result;

I want to get a collection of above both.

How to do that?

Thanks in advance.

1

I found a solution. My new code is following:

$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$ConfigureProducts = $read->fetchAll("SELECT * FROM `catalog_product_entity` 
NATURAL JOIN `catalog_product_flat_1`
where entity_id in (SELECT catalog_product_super_link.parent_id FROM `catalog_product_super_link` ) and type_id='configurable'");

$SimpleProduct = $read->fetchAll("SELECT * FROM `catalog_product_entity` 
NATURAL JOIN `catalog_product_flat_1`
where entity_id not in (SELECT product_id FROM `catalog_product_super_link`) and type_id='simple'");

$AllProducts = array_merge($ConfigureProducts,$SimpleProduct);
foreach($AllProducts as $value)
{
    $title = $value['name'];
    $entity_id = $value['entity_id'];
    $result[] = array('value'=>$entity_id,'label'=>$title);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.