Tuesday, July 31, 2012

Get Attribute Name And Value Magento


In magento you can create as many custom attributes for your products as you want.Suppose you want to add or display brand color for every product on home, category or product page. So this is very easy to do with custom attributes.
If we were to create a new custom attribute called “brand_color” and the attribute was of a “Text Field” type, we could do something like the following at a product level to obtain its value.
1
echo $_product->getBrandColor(); ?>
Get attribute collection
1
$attribute = $_product->getResource()->getAttribute('my_attribute'); ?>
Get attribute type
1
$attribute = $_product->getResource()->getAttribute('my_attribute')->getAttributeType(); ?>
Get attribute Label
1
$attribute = $_product->getResource()->getAttribute('my_attribute')->getFrontendLabel(); ?>
Attribute is visible or not
1
$attribute = $_product->getResource()->getAttribute('my_attribute')->getIsVisible(); ?>
Attribute is required
1
$attribute = $_product->getResource()->getAttribute('my_attribute')->getIsRequired(); ?>
Get attribute value
1
$attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getMyAttribute();?>
Get the multi-select attribute values
1
2
3
4
5
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','attribute_code_here');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
?>
Get specific attribute value
1
2
3
4
5
$attributeValue = Mage::getModel('catalog/product')
                            ->load($_product->getId())
                            ->getAttributeText('my_attribute');
?>