Magento Tutorials

How to Get Product Attribute with Name and Value in Magento

In Magento, attributes consist 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.

Magento 2 Import Export Product Attributes

Import Export Product Attributes is an additional extension for Magento that allows admin to quickly import and export bulk product attributes via a CSV file with one click.

How to get product attribute in Magento

Get all attributes of a product

$attributes = $product->getAttributes();
foreach($attributes as $a)
    echo $a->getName()."\n";

Get products if you know the product ID

productRepository is a new model introduced in Magento 2, using which you can get products by SKU or ID like so:

//get by SKU
//get by ID

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

* 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')

See also: How to Get All Products Grammatically in Magento


A knowledge craver who always strive to be wiser everyday.