Question

i want collection of those product which remain as simple product.

in other world i got all simple product which is include in configurable now i just want those product which does not included in configurable and still alone.

Was it helpful?

Solution

This is just an adaptation of what Amit Bera answered.
I just took the code and transformed it into one single query instead of 2.
The idea is to join left the products table with the table that keeps the relation between simple and configurable, and filter only the products that don't have such a relation:

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

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

now you just need to loop through the collection.

foreach ($collection as $product) {
     //do something with $product
}

OTHER TIPS

Magento has simple and configurable product relation in catalog_product_super_link table. So you can use advantage from this table.

Basically what you have to do is, filter the simple product collection by the product ids present in catalog_product_super_link table

Step1: Get list of all simple products by using below code

$collectionSimple = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'));

$collectionSimple ->getSelect()->join(array(
    'link_table' => 'catalog_product_super_link'),
    'link_table.product_id = e.entity_id',
    array('product_id')
));

$IdsSimpleCon=$collectionSimple->getAllIds();

enter image description here

Step2: Make another product Collection exclude of step one product collection and code

$NonConfigurablecollectionSimple = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'))
    ->addAttributeToFilter('entity_id',array('nin'=>$cond ));

echo $NonConfigurablecollectionSimple->getSelect(); 

Now, $NonConfigurablecollectionSimple would hold all those simple product which does not have a single relationship with configurable products.

you can use the type id to check simple or configurable product. Please refer the below code

$product->getTypeId() == "simple"

Just to complete on what Marius answered, this works for Grouped products:

$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_link'),
  'link_table.linked_product_id = e.entity_id',
  array('product_id')
);
$collection->getSelect()->where('link_table.product_id IS NULL');

now you just need to loop through the collection.

foreach ($collection as $product) {
     //do something with $product
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top