5

I've got a list of product ids and I want to load a product collection for the list:

$getdata[$a]=Array
(
[0] => 166
[1] => 166
[2] => 164
);
 $_productCollection =Mage::getModel('catalog/product')->getCollection()                               
     ->addAttributeToFilter('id', array('in' => $getdata[$a]))              
                               ->load();


I am getting error as
Fatal error: Call to a member function getBackend() on boolean in /var/www/magento//app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

17

try this

$getData= array(166,167,164);
$productCollection = Mage::getModel('catalog/product')->getCollection()
            ->addFieldToFilter('entity_id', array('in'=> $getData));
$productCollection->load();

echo count($productCollection);

Make sure your array $getData bellow format

$getData= array(166,167,164); 

and above array output like

Array ( [0] => 166 [1] => 167 [2] => 164 )
| improve this answer | |
  • var_dump($getdata[$a] gives me string(7) "166,166,164" – zest Jul 16 '16 at 12:26
  • you must be pass in array format – Abdul Jul 16 '16 at 12:30
  • first you convert str to array for eg. $getdata[$a] = "166,167,164"; $getdata[$a] = explode(",",$getdata[$a]); and after pass in $productCollection = Mage::getModel('catalog/product')->getCollection() ->addFieldToFilter('entity_id', array('in'=> $getdata[$a])); $productCollection->load(); – Abdul Jul 16 '16 at 12:36
2

Replace your code with:

$getdata[$a] = array
(
    [0] => 166,
    [1] => 166,
    [2] => 164
);
$_productCollection = Mage::getModel('catalog/product')
 ->getCollection()                
 ->addAttributeToFilter('entity_id', array('in' => $getdata[$a]))
| improve this answer | |
  • when i use this code it doesn't show any product – zest Jul 16 '16 at 11:45
  • Also, there is an error in creating $getdata array. Check updated answer and try – Anil Suthar Jul 16 '16 at 12:21
1

You filter by 'entity_id' not 'id'

$productCollection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToFilter('entity_id', array('in'
                     $getdata[$a]))->load();
| improve this answer | |
  • thanks for your answer when I use id instead of entity_id it throws same error – zest Jul 16 '16 at 11:09
1

Addition to the above answers. Change entity_id to id. It worked for me in Magento 1.7.0.1

   ->addAttributeToFilter('id', array('in' => $productIds));
| improve this answer | |
0

try with below code:

$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('entity_id', array('in' => $productIds));


$products->getSelect()->order("find_in_set(entity_id,'".implode(',',$productIds)."')");

foreach($products as $product)
{
    echo $product->getEntityId();
    echo $product->getSku();
}
| improve this answer | |

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.