Thursday, 18 October 2012

How to get attribute value of product in magento

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 pro-grammatically.
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.

  1. Mage::getModel('catalog/product')->load($product_id)->getAttributeText("size")  
  2.   
  3. // Change the attribute code here.  
  4. $attribute=$product->getResource()->getAttribute("color");  
  5.   
  6. // Checking if the attribute is either select or multiselect type.  
  7. if($attribute->usesSource()){  
  8.   
  9. // Getting all the sources (options) and print as label-value pair  
  10. $options = $attribute->getSource()->getAllOptions(false);  
  11. print_r($options);  
  12. }  
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.

  1. $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. 1. $CurtAtr  is  current attribute like color, size, etc  
  2. 2. $attrVal is attribute value e.g. size has "small, medium, Large, etc"  
  3.   
  4. $resource = Mage::getSingleton('core/resource');  
  5. $read = $resource->getConnection('catalog_read');  
  6. $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 !!

  1. $product = $this->getProduct();  
  2. $attrs  = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);  
  3. foreach($attrs as $attr) {  
  4.     if(0 == strcmp("color"$attr['attribute_code'])) {  
  5.         $options    = $attr['values'];  
  6.     foreach($options as $option) {  
  7.         echo $option[];  
  8.     }  
  9.     }  
  10. }  

1 comment: