How to get attribute name and value in Magento CMS?

Attribute in Magento resembles a property. All Products, Categories, Orders, Customers, and so on have traits. For instance, the characteristic of an item is its name, sku, depiction, picture, and so forth.
This article will demonstrate to you best practices to get Attribute name and incentive for any item.

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
29
30
<?php
/**
 * 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();
?>

Get value from a select box attribute
The attribute code is supposed to be ‘my_attribute‘

1
2
3
$attributeValue = Mage::getModel('catalog/product')
            ->load($_product->getId())
            ->getAttributeText('my_attribute');

Load any particular attribute by attribute code

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

Load any particular attribute by attribute code : Another way
In the code below, we provide the entity type and attribute code to the loadByCode function.
$entityType can be either “entity_type_id” OR “entity_type_code” OR instance of “Mage_Eav_Model_Entity_Type” class.
Here, we will be fetching “color” attribute of “product”.

1
2
3
4
5
$entityType = 'catalog_product';
$attributeCode = 'color';
$attributeInfo = Mage::getModel('eav/entity_attribute')
                ->loadByCode($entityType, $attributeCode);
// echo "<pre>"; print_r($attributeInfo->getData());

Get all option value list for the particular attribute
You can see above that I got attribute information by attribute code. My attribute information is stored as $attributeInfo. See code above.
Here is the code to get all option values for my attribute $attributeInfo.

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
I have my attribute as $attributeInfo.
I have my attribute’s option value array as $attributeOptions.
See code above.
Suppose, I want to get detail information of any option listed in strong>$attributeOptions array. Here is the code to do so:-

1
2
3
4
5
6
7
8
9
10
$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
Here, I am going to get information about ‘order_id‘ attribute of ‘invoice‘ entity type.

1
2
3
4
5
6
$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 of particular entity type : Another way
Here, we are fetching information about “color” attribute of “product”.
$entityType can be either “entity_type_id” OR “entity_type_code” OR instance of “Mage_Eav_Model_Entity_Type” class.
The “entity_type_id” or “entity_type_code” can be found in database table eav_entity_type.

1
2
3
4
5
$entityType = 'catalog_product';
$attributeCode = 'color';
$attributeInfo = Mage::getModel('eav/entity_attribute')
                ->loadByCode($entityType, $attributeCode);
// echo "<pre>"; print_r($attributeInfo->getData());

Get attribute options of Configurable product

Leave a Reply

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