Getting product attributes values and labels

I have included few code snippets which will guide you on how to get attribute code, attribute value, attribute label. Being a Magento developer its little difficult specially for beginner to get attribute values and labels.  e.g. Options of color or manufacturer or size attribute, etc.

You may also need to find out what are the different options available for color attribute before you add new option programmatically.
Or you may need to add assign the option’s ID to a product. Here’s what you need to do.

Few Qick and Easy Ways, isn’t it! Code is below.

// Lets say $_product is the product object.
$_attributes = Mage::helper('core')->decorateArray($_product->getAllowAttributes());
foreach($_attributes as $_attribute):
// Get Attribute Code
$attCode = $_attribute->getProductAttribute()->getFrontend()->getAttribute()->getAttributeCode();
// Get Attribute Id
$attrId =  $_attribute->getAttributeId();
foreachend;

In following example $product_id is current product, for whom you want to fetch attribute value.

Mage::getModel('catalog/product')->load($product_id)->getAttributeText("size")
// Change the attribute code here.
$attribute=$product->getResource()->getAttribute("color");
// Checking if the attribute is either select or multiselect type.
if($attribute->usesSource()){
// Getting all the sources (options) and print as label-value pair
$options = $attribute->getSource()->getAllOptions(false);
print_r($options);
}

How do i get selected value for a attribute?
The following line will return you the selected value for the attribute. Replace variable $product_id with actual product id.

$attrValue = Mage::getModel('catalog/product')->load($product_id)->getAttributeText('color');

How to fetch attribute value sort order?
Here is the Qick and Easy hack on how to get attribute value sort positing.
Change value for variables in following code

1. $CurtAtr  is  current attribute like color, size, etc
2. $attrVal is attribute value e.g. size has "small, medium, Large, etc"
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$read->fetchAll("SELECT ao.sort_order FROM mage_eav_attribute_option_value as aov, mage_eav_attribute_option as ao where value='$attrVal' and aov.option_id = ao.option_id and attribute_id=$CurtAtr limit 1");

 

magento configurable product – how to get available values for color attribute?
In following snippet i have fetched color attribute values just change color to other attribute you should be able to get value for other multi select attribute in magento. Bingo !!

$product = $this->getProduct();
$attrs  = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach($attrs as $attr) {
    if(0 == strcmp("color", $attr['attribute_code'])) {
        $options    = $attr['values'];
	foreach($options as $option) {
		echo $option[];
	}
    }
}
Share this nice post:

15 thoughts on “Getting product attributes values and labels

  1. I harmonise with your conclusions and will-power thirstily look progressive to your coming updates.

  2. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  3. I do enjoy the manner in which you have presented this specific difficulty and it does provide me some fodder for thought.

  4. I wanted to get the number of colors and sizes of each product on my category pages and your code did the trick! Thank you!

  5. Tammy this a wonderful blog put up! It is so wonderful when people today appreciate all that you do.

Comments are closed.