Sergey
Sergey

Reputation: 258

magento product collection

In extention I'm getting product collection on catalog_product_view page like this:

if (!$product = Mage::registry('product')) {
    return new Varien_Data_Collection();
}
$category = new Mage_Catalog_Model_Category();
$category->load($product->getCategoryId());
$collection = $category->getProductCollection();

and how can I add additional attributes to this collection?

for example I can't get something like this

$collection->addAttributeToSelect('manufacturer');

I wish to add some attribute by code (not id, because this may be different attributes added in layout) and then group data by this attribute

thnx

Upvotes: 1

Views: 3295

Answers (2)

Swapna Taru
Swapna Taru

Reputation: 688

Try this

<?php echo $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product); ?>

Upvotes: 0

Simon
Simon

Reputation: 4192

You could instantiate a product collection and filter it instead of getting the products of a specific category directly:

if (!$product = Mage::registry('product')) {
    return new Varien_Data_Collection();
}
// get a product collection and filter it by category
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($product->getCategoryId());
// add the attributes you need
$collection->addAttributeToSelect('manufacturer');
$collection->setOrder('manufacturer', 'asc');
// load the collection and return it
$collection->load();
return $collection;

Be careful: You can not add attributes to the collection after loading it! Additionally, you do not have to explicitely call load() - the collection will be loaded on demand.

Hope this helps.

Upvotes: 1

Related Questions