4

I have a statement

$collection = Mage::getResourceModel('catalog/product_collection');

in file app/code/core/Mage/Catalog/Block/Product/New.php in protected function _getProductCollection().

It gives me list of common attributes when I check by

print_r($collection->getData()); 

Here I need additional attribute value for attribute 'color'

Some products contain attribute color and some not. When I use the statement $collection->addAttributeToSelect('color', 'color'); it gives me only product with attribute color

I need all products + products with attribute color

please help me.

  • use $collection->addAttributeToSelect('*'); – Mufaddal Jul 10 '15 at 7:31
  • it gives only common attribute(entity_id, sku etc ) not attribute color. – divyang Jul 10 '15 at 7:34
  • 2
    iterate collection object in foreach and see – Mufaddal Jul 10 '15 at 7:36
3

To get product collection with all attributes use this:

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

Whereas to get all products having attribute color only use

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('color','color');
1

To include additional attributes in standard product lists like in Mage_Catalog_Block_Product_New, follow these steps:

  1. Go to Catalog > Attributes > Manage Attributes and select the attribute, you want to include.

  2. Set "Used In Product Listing" to "Yes":

    screenshot

  3. If you use the flat catalog indexes, rebuild them.


If you use custom code to initialize a product collection, make sure to use

$collection->addAttributeToSelect(
    Mage::getSingleton('catalog/config')->getProductAttributes());

instead of

$collection->addAttributeToSelect('*');

This way only the attributes, configured as above will be loaded, using the flat index if available. Selecting all attributes with "*" is slow with Magentos EAV models.

0

To get all the products with all the attributes, use this -

$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $_product) {
   echo "<pre>";print_r($_product->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.