Magento Tutorials

How to Get Product Attribute with Name and Value in Magento

Magento product attribute

In Magento, attribute consists of one or more name & value pairs. For products, the attributes are name, description, sku, image, etc. assigned with their own value. This guide is gonna show you how to get product attributes in Magento.

How to get product attribute in Magento

Get specific product attribute value if you know the product ID

Mage::getResourceModel(‘catalog/product’)->getAttributeRawValue($productId, ‘attribute_code’, $storeId);

Get attribute’s name, value, type, and other parameters

* get attribute collection
$attribute = $_product->getResource()->getAttribute(‘my_attribute’);
* get attribute type
* get attribute Label
* get attribute default value
* check if the attribute is visible
* check if the attribute is required
* get attribute value
$attributeValue = Mage::getModel(‘catalog/product’)->load($_product->getId())->getMyAttribute();

Load any particular attribute by attribute code

$attributeInfo = Mage::getResourceModel(‘eav/entity_attribute_collection’)

// echo “<pre>”; print_r($attributeInfo->getData());

Get all option value list for the particular attribute

$attributeOptions = $attributeInfo->getSource()->getAllOptions(false);
// echo “<pre>”; print_r($attributeOptions);

Get all options of any attribute

(Getting all options of attribute with attribute-code “color“.)

$attribute = Mage::getSingleton(‘eav/config’)->getAttribute(‘catalog_product’, ‘color’);
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);

Get attribute’s option information by option id

$attributeId = $attributeInfo->getAttributeId();

$attributeOptionSingle = Mage::getResourceModel(‘eav/entity_attribute_option_collection’)

// echo “<pre>”; print_r($attributeOptionSingle);

Get attribute of particular entity type

(Get information about ‘order_id‘ attribute of ‘invoice‘ entity type.)

$entityType = Mage::getModel(‘eav/config’)->getEntityType(‘invoice’);
$entityTypeId = $entityType->getEntityTypeId();

$attribute = Mage::getResourceModel(‘eav/entity_attribute_collection’)

Get attribute options of Configurable product

$confAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);

See also: How to Get All Products Grammatically in Magento

A knowledge craver who always strive to be wiser everyday.

Notify of
Inline Feedbacks
View all comments