Getting product attributes values and labels

30 篇文章 0 订阅

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.

  1. // Lets say $_product is the product object.  
  2. $_attributes = Mage::helper('core')->decorateArray($_product->getAllowAttributes());  
  3. foreach($_attributes as $_attribute):  
  4. // Get Attribute Code  
  5. $attCode = $_attribute->getProductAttribute()->getFrontend()->getAttribute()->getAttributeCode();  
  6. // Get Attribute Id  
  7. $attrId =  $_attribute->getAttributeId();  
  8. 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. // Change the attribute code here.  
  3. $attribute=$product->getResource()->getAttribute("color");  
  4. // Checking if the attribute is either select or multiselect type.  
  5. if($attribute->usesSource()){  
  6. // Getting all the sources (options) and print as label-value pair  
  7. $options = $attribute->getSource()->getAllOptions(false);  
  8. print_r($options);  
  9. }  

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. $resource = Mage::getSingleton('core/resource');  
  4. $read = $resource->getConnection('catalog_read');  
  5. $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 inmagento. 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. }  

http://www.phptechi.com/getting-product-attributes-values-and-labels.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值