2

I've got a big product collection about 68.000 items, when I loop through the collection I'm trying to get all attributes by using $collection->getAttributes() but it returns nothing.

When I load the products attributes like

$catId=604;
$collection = Mage::getModel('catalog/category')->load($catId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', $catId)
    ->addAttributeToFilter('type_id', 'simple')
    ->addAttributeToFilter('status', 1)->load();

foreach($collection as $col) {
    $prod = Mage::getModel('catalog/product')->load($col->getId());
    $prod->getAttributes();
}

the performance is horrible so I want to get the attributes by using the collection but it doesn't work at all.

Any suggestions ?

  • Can you add the piece of code where you create the collection? Like $collection = Mage::getModel('catalog/product')->getCollection()->........ – 7ochem Apr 10 '15 at 12:27
  • $catId=604; $collection = Mage::getModel('catalog/category') ->load($catId) ->getProductCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('category_id', $catId) ->addAttributeToFilter('type_id', 'simple') ->addAttributeToFilter('status', 1)->load(); – Cenzo Apr 10 '15 at 12:32
  • Can you use the catalog/product_attribute_collection resource model instead? – Douglas Radburn Apr 10 '15 at 12:38
  • How to switch to the product_attribute_collection by using getProductCollection on category model ? – Cenzo Apr 10 '15 at 12:42
1

You cannot call getAttributes() on $collection as this is the collection model/object. In your code example you are calling getAttributes() on $prod which is a product model/object.

You should remove the line $prod = Mage::getModel('catalog/product')->load($col->getId()); which is causing a huge load because you are loading every individual product again while they were already loaded.

Just call $col->getAttributes(); right away.

By the way, as far as I can see, getAttributes() just returns all editable attributes of the product EAV model. So this array is the same for all of your 68.000 products (per attribute set that is). If you need all values in an array, just do $col->getData()

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.