Magento Forum

   
Retrieve a list of products by excluding some others
 
sepan77
Jr. Member
 
Total Posts:  9
Joined:  2012-01-29
 

I’m trying to get a collection of products and want to exclude some products by an array of id’s.

The following code gives me an error:

( ! ) Fatal error: Call to a member function getBackend() on a non-object in /Applications/MAMP/htdocs/testmage/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816
Call Stack
# Time Memory Function
1 0.2469 100104 {main}( ) ../test1.php:0
2 3.9960 4215264 Mage_Catalog_Model_Resource_Product_Collection->addAttributeToFilter( ???, ???, ??? ) ../test1.php:9
3 3.9962 4219504 Mage_Eav_Model_Entity_Collection_Abstract->addAttributeToFilter( ???, ???, ??? ) ../Collection.php:1418
4 3.9963 4222176 Mage_Eav_Model_Entity_Collection_Abstract->_getAttributeConditionSql( ???, ???, ??? ) ../Abstract.php:321
5 3.9963 4225040 Mage_Eav_Model_Entity_Abstract->isAttributeStatic( ??? ) ../Abstract.php:1379

The code is:

$exclude = array(15537,15536,15535); // exclude these product by their id
$_products Mage::getModel('catalog/product')
        ->
getCollection()
        ->
addAttributeToSelect('*')
        ->
addAttributeToFilter(
            
'id',
            array(
'nin'$exclude)
        );

 
Magento Community Magento Community
Magento Community
Magento Community
 
VortexCommerce
Sr. Member
 
Avatar
Total Posts:  104
Joined:  2008-08-06
Harrogate, UK
 

Hi, try this:

$exclude = array(15537,15536,15535);
$_products Mage::getModel('catalog/product')->getCollection()
        ->
addAttributeToSelect('*')
        ->
addAttributeToFilter'entity_id', array('nin'$exclude));

or

$exclude = array(15537,15536,15535);
$_products Mage::getModel('catalog/product')->getCollection()
        ->
addAttributeToSelect('*')
        ->
addIdFilter($excludetrue);

 
Magento Community Magento Community
Magento Community
Magento Community
 
kdtips
Member
 
Avatar
Total Posts:  73
Joined:  2011-08-13
Auckland New Zealand
 

Yes this one works....

$exclude = array(15537,15536,15535);
$_products Mage::getModel('catalog/product')->getCollection()
        ->
addAttributeToSelect('*')
        ->
addAttributeToFilter'entity_id', array('nin'$exclude));

 
Magento Community Magento Community
Magento Community
Magento Community
 
sepan77
Jr. Member
 
Total Posts:  9
Joined:  2012-01-29
 

When I execute:

$exclude = array(15537,15536,15535);
$_products Mage::getModel('catalog/product')
    ->
getCollection()
    ->
addAttributeToSelect('id')
    ->
addAttributeToFilter'entity_id', array('nin'$exclude));
I get products with id 15537,15536,15535, but want I want is to load all products without those with ID 15537,15536,15535.
The array $exclude should indicate an exclusion list.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sepan77
Jr. Member
 
Total Posts:  9
Joined:  2012-01-29
 

This one works for me as an exclusion list:

VortexCommerce - 13 November 2012 11:06 AM

Hi, try this:

$exclude = array(15537,15536,15535);
$_products Mage::getModel('catalog/product')->getCollection()
        ->
addAttributeToSelect('*')
        ->
addIdFilter($excludetrue);

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top