Sunday, February 8, 2015

Get attributes of a attribute set in Magento

Method 1:

$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);

foreach($attributes as $_attribute){
  print_r($_attribute);
}

Method 2:

You don't necessarily need to access the API class. There is a more natural approach available. If you have a product:

/** @var Mage_Catalog_Model_Product $product **/
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);

If not:

$attributes = Mage::getModel('catalog/product')->getResource()
  ->loadAllAttributes()
  ->getSortedAttributes($attributeSetId);

Reference:

http://stackoverflow.com/questions/18627559/get-the-attributes-for-attribute-set-in-magento
http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/

No comments: