Monday, March 1, 2010

Retrieve attributes by groupId

Problem: how to retrieve product attributes by group id.
See here: http://www.magentocommerce.com/boards/viewthread/79694/

Possible solution:
<?php
require_once 'path/to/app_dir/app/Mage.php';
Mage::app('default');
$product = Mage::getModel('catalog/product');
$product->setAttributeSetId('THE ATTRIBUTE SET YOU WANT TO USE');
$groupId = 'THE ID OF THE GROUP YOU WANT TO RETRIEVE';

$collection = $product->getAttributes($groupId, false);
foreach($collection as $attribute){
//do something with the attribute
}
?> 

3 comments:

  1. This post is very interesting. How can create a table including in the first row the attribute labels and the attribute values in the second row?

    ReplyDelete
  2. Replies
    1. As you can see this post is more than 3 years old. I don't remember the Magento version I did this for. I will check for 1.7 maybe I can find something.

      Delete