Magento Tutorials

How to Get Product Attribute with Name and Value in Magento

magento-get-product-attribute

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 product ID

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * get attribute collection
 */

$attribute = $_product->getResource()->getAttribute('my_attribute');
/**
 * get attribute type
 */

$attribute->getAttributeType();
/**
 * get attribute Label
 */

$attribute->getFrontendLabel();
/**
 * get attribute default value
 */

$attribute->getDefaultValue();
/**
 * check if the attribute is visible
 */

$attribute->getIsVisible();
/**
 * check if the attribute is required
 */

$attribute->getIsRequired();
/**
 * get attribute value
 */

$attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getMyAttribute();

Load any particular attribute by attribute code

1
2
3
4
5
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
 ->setCodeFilter(YOUR_ATTRIBUTE_CODE)
 ->getFirstItem();
 
// echo "<pre>"; print_r($attributeInfo->getData());

Get all option value list for the particular attribute

1
2
$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“.)

1
2
3
4
$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

1
2
3
4
5
6
7
8
9
10
11
12
$attributeId = $attributeInfo->getAttributeId();
$optionId = YOUR_ATTRIBUTE_OPTION_ID;
 
$attributeOptionSingle = Mage::getResourceModel('eav/entity_attribute_option_collection')
 ->setPositionOrder('asc')
 ->setAttributeFilter($attributeId)
 ->setIdFilter($optionId)
 ->setStoreFilter()
 ->load()
 ->getFirstItem();
 
// echo "<pre>"; print_r($attributeOptionSingle);

Get attribute of particular entity type

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

1
2
3
4
5
6
7
$entityType = Mage::getModel('eav/config')->getEntityType('invoice');
$entityTypeId = $entityType->getEntityTypeId();
 
$attribute = Mage::getResourceModel('eav/entity_attribute_collection')
 ->setCodeFilter('order_id')
 ->setEntityTypeFilter($entityTypeId)
 ->getFirstItem();

Get attribute options of Configurable product

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

 

See also: How to Get All Products Grammatically in Magento

How to Get Product Attribute with Name and Value in Magento
5 (100%) 3 votes
0 Comments

Dom

A knowledge craver who always strive to be wiser everyday.

    Reply your comment

    Your email address will not be published. Required fields are marked*