Call us Today! 561-299-5932

What are Attributes in Magento?

SEO GazelleGazelle Coding Our ThoughtsWhat are Attributes in Magento?
Posted by:

In Magento, Attributes are quantifiable or descriptive aspects of a product, from the color, to the manufacturer, to the SKU number. There are two kinds of attributes in Magento: System Attributes and Custom Attributes.

By default, Magento includes all the necessary System Attributes. These cannot be deleted, and every product must have each of these attributes. Therefore, all Attribute Sets must include these attributes (more on Attribute sets in the next section). These type of attributes include Name, Price, and SKU, without which a product would not function.

Adding Custom Meta Attributes in Magento, on the other hand, are attributes created by the Store Owner. These will likely be more specific for individual products, and therefore will not necessarily be included in every Attribute set. For example, if you were to create an attribute for color, with values of blue, green, yellow, etc., you may want to apply this attribute to an Attribute Set for shirts, which you sell in various colors, but you probably would not include it in an Attribute Set for DVDs, for which color is not really an applicable attribute.

FTP Hierarchy: public_html/app/design/frontend/<theme name>/template/page/html/<file name>

The attribute of a product is its name, sku, description, image, ect. This article will show you how to get attribute name and value for any product.

The following code is about getting attribute collection and then fetching that attribute’s name, value, type, and other parameters that you may need when customizing your Magento website theme.

Note: The attribute code in the case below is my_attribute.

/**
 * 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();
 Here is the code to fetch value from a select box attribute:-

SPECIAL NOTE: I suppose the attribute code to be ‘my_attribute‘

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

Load any particular attribute by attribute code
Below is the code to get data of any particular attribute by the attribute code from your Magento website.

$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
                        ->setCodeFilter(YOUR_ATTRIBUTE_CODE)
                        ->getFirstItem();
// echo"; print_r($attributeInfo->getData()); echo "

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.

Here is the code to get all option values for my attribute $attributeInfo.

$attributeOptions = $attributeInfo->getSource()->getAllOptions(false);
 // echo "; print_r($attributeOptions); echo ";

Get attribute’s option information by option id

I have my attribute as $attributeInfo.
I have my attribute’s option value array as $attributeOptions.

Suppose, I want to get detail information of any option listed in $attributeOptions array. Here is the code to do so:-

$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 "; print_r($attributeOptionSingle); echo ";

Get attribute of particular entity type:

Here, I am going to 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')
 ->setCodeFilter('order_id')
 ->setEntityTypeFilter($entityTypeId)
 ->getFirstItem();

Get attribute options of Configurable product

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

I Hope this article helps when assigning and locating your attributes in Magento. Thanks!

Comments are closed.

FacebookTwitterRSSLinkedinPinterestYouTube