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.
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.
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
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 !!
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.- 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);
- }
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');
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[];
- }
- }
- }
This comment has been removed by the author.
ReplyDelete