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.
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.
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
}
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();
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.
catalog_product_super_link
. I never used this table before.
Commented
Feb 20, 2015 at 0:52
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
}