Magento: Getting product attributes values and labels
I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. I always forget the exact syntax to use so, this is going to be my unofficial cheat sheet.
This is how to get a drop down lists options. I don’t think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attributeArray[$option['value']] = $option['label'];
}
I had a trickier time getting values for a multi-select attribute. I don’t think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.
$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
if($a->getAttributeCode() == 'desired_attribute_code'){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
}
Here is a better way to get the multi-select attribute values.
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product','attribute_code_here');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
If you only need to retrieve a value for a product you can try this way
//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)
I added this code snip it below to possibly answer a question that was posted. The code below will get an attribure collection. Set {entityType} to 4 to get attributes for products. You can remove the “setCodeFilters” line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or something.
//
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter({entityType})
->setCodeFilter($attributes)
->addSetInfo()
->getData();
Entity Type Id’s
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.
1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity
The Following code was added to answer a question in the replies:
To make a product attribute avaliable when getting a product collection(such as on a category page or search results page) you can add some code the a config.xml file that instructs Magento to load allways the attribute when a product collection is loaded.
How to add an attribute to be loaded whenever a product collection is loaded:
This can go in any config.xml file. I would recommend putting it in a custom module rather than one from the core code. Just replace “attribute_name” with the attribute code of the attribute you are trying to add.
How to get Only the Attribute Values that have been used on products
The Code below will get the attribute values that have been used on the products in the product collection.
This may not be the best way to do this, but it is what I came up with.
//First get the attribute id
$audienceAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','session_audience');
//Now Load the attribute
$audienceAttribute = Mage::getModel('catalog/resource_eav_attribute')->load($audienceAttributeId);
//Now get the product collection that you want to use to fine the attributes values for.
//I wanted the attribute values for only grouped products. You could add category filters and such
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter(Mage::app()->getStore())
->addAttributeToSelect('session_audience')
->addAttributeToSort('session_audience', 'asc')
->addAttributeToFilter('type_id', array('eq' => 'grouped'));
//Now get the product ids for the collection
$productCollectionIds = $productCollection ->getAllIds();
//Now we query the data base to get the attribute values for the given product ids.
//NOTE that I am selecting from the catalog_product_entity_varchar table because this is the type of attribute I was using.
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select();
$select->from('catalog_product_entity_varchar')
->where('attribute_id = ?', $audienceAttributeId)
->where('entity_id IN (?)', $productCollectionIds );
//print_r($select->__toString());die();
//Now get the ids for the attribute values.
$result = $read->query($select);
$attributeOptionIds = array();
while($row = $result->fetch()){
$attributeOptionIds = array_merge($attributeOptionIds, explode(',', $row['value']));
}
array_unique($attributeOptionIds);
//print_r($audienceOptions);die();
//Now get the actual values for the Ids as a Collection and do something with the values.
$filteredAudienceCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($audienceAttributeId)
->setStoreFilter($audienceAttribute->getStoreId())
->addFieldToFilter('main_table.option_id', array('in' => $attributeOptionIds))
->load();

79 Responses to “ Magento: Getting product attributes values and labels ”
April 22nd, 2009 at 5:26 am
You are my hero today!
Thanks a lot!
June 18th, 2009 at 10:48 am
wow! the 2nd method works fine!!
I use this to check if attribute exits too:
if (!$id = $eav_entity_setup->getAttribute($entity_type_id, $code, ‘attribute_id’)) {
return false;
}
thanks!
July 28th, 2009 at 12:35 am
Oh! Thank you ! You save my time.
August 30th, 2009 at 5:48 pm
Thank you! I have been trying to find this code for the last few days.
Nick
September 7th, 2009 at 12:18 pm
Thanks, do you have any idea how to get attributes by attribute_id which only relate to existing products in DB.
I’ll try to display it as filters (so if no products have assigned attribute, attribute not display).
September 7th, 2009 at 8:14 pm
Added reference to code that gets a collection of attributes.
September 17th, 2009 at 12:36 am
thanks dude
September 21st, 2009 at 5:15 pm
It would be helpful if you could post a further description of what $entityType is (from the last example) and why 4 is the value to use for products.
September 22nd, 2009 at 7:08 am
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.
1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity
and so on. THese four ids are the ones that you will commonly use.
November 12th, 2009 at 10:44 am
thanks very much!
I used this: $_product->getResource()->getAttribute(‘club_type’)->getFrontend()->getValue($_product)
but for total noobs (like myself) it is worth to mention that you need to add “echo” in front of the line to actually display the value of the attribute: so in your code you will have:
getResource()->getAttribute(‘name_of_the_attribute’)->getFrontend()->getValue($_product) ?>
hope this helps someone totally lost,
Kamil
November 17th, 2009 at 10:09 am
[...] compléter cet article, voici un lien assez intéressant permettant d’afficher tous les attributs automatiquement, etc. [...]
November 25th, 2009 at 9:39 pm
For the multi-select, if you’re within a Mage_Catalog_Block_Product_View template (ie view.phtml) then you don’t need the advanced search visibility to be set or referenced, this works as well:
$attributes = $_product->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
if($a->getAttributeCode() == 'your_code_here'){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
}
December 29th, 2009 at 9:19 am
Hi
thanks for this, which is pretty clear, but I dont get anything when I code this:
public function test_ProductData_DefaultProductSize_ShouldBeT1()
{
// Actors
$_product;
$optionCode ="size";
$expectedValue = "T1";
$actualValue = "QUELLE EST CETTE TAILLE";
$productId = "4b38dffad30c6";
// Activities
$_product= null;
$_product = new Mage_Catalog_Model_Product();
$_product = Mage::getModel('catalog/product')->load($productId);
$actualValue = $_product->getResource()
->getAttribute($optionCode)
->getFrontend()
->getValue($_product);
// Asserts
$this->assertEquals($expectedValue, $actualValue, "ASSERT FAILURE: attribute $optionCode is wrong.");
}
any idea of what I could have missed?
December 29th, 2009 at 9:21 am
I get this display:
There was 1 failure:
1) Moz_Product_ProductTestAttributes::test_ProductData_DefaultProductSize_ShouldBeT1
ASSERT FAILURE: attribute size is wrong.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-T1
+No
December 29th, 2009 at 11:01 am
@J: I believe you are loading the product incorrectly. After you load the product, does “$_product” have an ID “$_product->getId()”?
December 30th, 2009 at 3:43 am
Thanks Mike D,
I rewrote the get of the product as :
public static function GetProductFromSku($skuId)
{
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('sku',$skuId)
->load();
if (0 === count($products)) {
throw new Exception("Error: Product $skuId not found");
}
$product = null;
foreach($products as $p) {
$product = $p;
break;
}
if(null === $product) {
throw new Exception("Error: Product $skuId not in result collection");
}
return $product;
}
and then my test is:
public function test_ProductData_DefaultProductSize_ShouldBeT1()
{
// Actors
$_product= null;
$optionCode ="size";
$expectedValue = "T1";
$actualValue = "QUELLE EST CETTE TAILLE";
$productId = "cdn0001";
// Activities
$_product = new Mage_Catalog_Model_Product();
$_product = common_ProductData::GetProductFromSku($productId);//Mage::getModel('catalog/product')->load($productId);
$actualValue = $_product->getResource()->getAttribute($optionCode)->getFrontend()->getValue($_product);
// Asserts
$this->assertNotNull($_product->getId(), "ASSERT FAILURE: Product id should be set !");
$this->assertTrue($_product->getId() > 0, "ASSERT FAILURE: Product id is ".$_product->getId());
$this->assertEquals("1", $_product['stock_item']['is_in_stock'], "ASSERT FAILURE: Product should be In Stock");
$this->assertEquals($expectedValue, $actualValue, "ASSERT FAILURE: attribute '$optionCode' is wrong.");
}
But the result is still:
There was 1 failure:
1) Moz_Product_ProductTestAttributes::test_ProductData_DefaultProductSize_ShouldBeT1
ASSERT FAILURE: attribute 'size' is wrong.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-T1
+No
C:\svn\moz\trunk\tests\integration\Moz\Product\ProductTestAttributes.php:27
FAILURES!
Tests: 1, Assertions: 5, Failures: 1.
When I parse the attributes with a foreach ($_product->getAttributes() as $attribute)
I can see the “Size”, but it is the label. How to get the value for the $_product ?
January 18th, 2010 at 2:31 am
I’m trying to retrieve attributes name (multilanguage) in the catalog list (when the products are listed in categories), is it possible using this method?
Because “$attribute->getSource()->getAllOptions(true, true)” returns me an empty array of this type:
“Array ( [0] => Array ( [label] => [value] => ) )”
Am I doing something wrong?
January 18th, 2010 at 8:01 am
What type of attribute is “multilanguage”?
1. I am assuming you created it?
2. Is it a text field, drop-down, …?
Are you just trying to display it for each product on the product list?
One way to do this would be to add that attribute to a config file so it always loads when Magento grabs the product collection. I have added sample code to the end of the post that demonstrates this.
January 19th, 2010 at 12:15 am
Here is the solution to get attribute label for multilanguage site:
$attributeLabel = $_product->getResource()->getAttribute(‘YOUR_ATTRIBUTE_CODE’)->getFrontendLabel();
$translationArray = Mage::app()->getTranslator()->getResource()->getTranslationArray();
echo $translationArray["Mage_Catalog::".$attributeLabel];
Hope this helps.
January 19th, 2010 at 11:02 pm
hay any one help me to list out all options and its values of particular product…
January 20th, 2010 at 8:26 am
@Bhavesh Dave: Are you trying to print all options for a particular attribute for a product or all attributes for a product?
if $product is your product then you list attributes by
$product->getAttributeName();
To see all the attributes available you can do print_r($product);
Does this help?
February 10th, 2010 at 9:25 am
THANK YOU!
You saved me a lot of time!
Now i can go home
March 31st, 2010 at 5:01 am
Nice Posts, but I have a different problem, I would like to add a foreign key to the product table. i.e. I wanted to filter the product according to the administrator who added the product from the administrator’s panel. i.e. I want to keep track of the products according to administrator’s role and permission. Is it possible to do so. If yes, how? Please respond. Thanx.
March 31st, 2010 at 6:46 am
@tuppi Sau: What you want to do sounds reasonable. You would need to create a module that adds the nessary attributes to the product then create an observer or override the methods for product create/update. If you are a beginner you might want to pay someone to build this.
Here are a few articles that should get you started.
For installing custom attributes with a module
To add the new attributes to the product grid you will more than likely need to override the block and template files for this.
April 27th, 2010 at 1:44 am
Is it possible to get all (non core) attributes in the magento shop? I need this to create attribute sets programatically.
April 27th, 2010 at 6:53 am
I don’t believe there is a method that will do exactly that. I am not sure why you would need to do that. If you are creating an attribute set then you should know what attributes you want to add to it so just use addAttributeToFilter(), for each attribute you want. Or explain what you are trying to do a little better.
May 26th, 2010 at 2:11 am
Hello,
I’m looking for a method to show all possible attributes and their values for some product, even those, which are not in stock. I have to show possible sizes and colors to the customer..
Any ideas how to add filtering by product ID to “get all attributes/values”?
Thanks.
May 26th, 2010 at 6:36 am
This might not be the easiest way, but what if you make an array of attributes with the key of the array being the attribute_code and the value being what type of attribute it is(select, multi-select, text field, ect.) Then do a foreach loop with the array and check to see if the current product has that attribute then if it does have the attribute, check to see which type of output it has(select, multi-select, text field, ect.) and use the appropriate code from the example above to retrieve the values.
I cant remember if there is a method that will just get all the attributes for a product.
May 26th, 2010 at 6:56 am
Thank you, Mike.
I also found another variant, looks like it works:
$possibleAttributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);June 5th, 2010 at 1:44 pm
Thanks, it worked and now can show the attribute option value in product listing.
June 29th, 2010 at 2:27 am
Thanks Mike, with your help now I see custom attributes on my landing pages!
July 12th, 2010 at 3:51 pm
[...] http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/ [...]
August 2nd, 2010 at 6:36 am
Thanks!!!
September 4th, 2010 at 12:17 pm
HI i have a Problem getting the Options of an Multiselect Attribute… I created the multiselect Attribute in the Adminpanel with two options. Now i tried this:
$attribute = Mage::getModel(‘eav/entity_attribute’);
$attribute->loadByCode(‘catalog_product’, “products_author_unique”);
$options = $attribute->getSource()->getAllOptions(false);
but “$attribute->getSource()” throws an error:
Fatal error: Call to a member function setAttribute() on a non-object in C:\xampp\htdocs\Magento\app\code\core\Mage\Eav\Model\Entity\Attribute\Abstract.php on line 374
a dump of my $attribute looks like this:
Mage_Eav_Model_Entity_Attribute Object
(
[_eventPrefix:protected] => eav_entity_attribute
[_eventObject:protected] => attribute
[_cacheTag:protected] => EAV_ATTRIBUTE
[_name:protected] =>
[_entity:protected] =>
[_backend:protected] =>
[_frontend:protected] =>
[_source:protected] =>
[_attributeIdCache:protected] => Array
(
)
[_dataTable:protected] =>
[_resourceName:protected] => eav/entity_attribute
[_resource:protected] =>
[_resourceCollectionName:protected] => eav/entity_attribute_collection
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[attribute_id] => 577
[entity_type_id] => 4
[attribute_code] => products_author_unique
[attribute_model] =>
[backend_model] => eav/entity_attribute_backend_array
[backend_type] => varchar
[backend_table] =>
[frontend_model] =>
[frontend_input] => multiselect
[frontend_label] => products_author_unique
[frontend_class] =>
[source_model] =>
[is_required] => 0
[is_user_defined] => 1
[default_value] => 6
[is_unique] => 0
[note] =>
[frontend_input_renderer] =>
[is_global] => 0
[is_visible] => 1
[is_searchable] => 1
[is_filterable] => 1
[is_comparable] => 1
[is_visible_on_front] => 0
[is_html_allowed_on_front] => 1
[is_used_for_price_rules] => 0
[is_filterable_in_search] => 1
[used_in_product_listing] => 1
[used_for_sort_by] => 0
[is_configurable] => 0
[apply_to] =>
[is_visible_in_advanced_search] => 1
[position] => 0
[is_wysiwyg_enabled] => 0
[is_used_for_promo_rules] => 0
)
[_hasDataChanges:protected] => 1
[_origData:protected] =>
[_idFieldName:protected] => attribute_id
[_isDeleted:protected] =>
)
September 5th, 2010 at 11:29 am
@Peter: Doesn’t look like you followed the directions for a MultiSelect Attribute.
Read Paragraph 3.
I had a trickier time getting values for a multi-select attribute. I don’t….
September 16th, 2010 at 10:26 am
Hi!
How can I get the attribute set id of a product?
I bet it’s simple, but I just can’t figure it out!
I have:
$product = Mage::registry(‘product’);
And what do I do next?
echo $product->getResource()->… ?
Thanks in advance,
HT
September 16th, 2010 at 10:32 am
I figured it out by making a ReflectionClass and displaying it’s methods.
$product->getAttributeSetId()
It was pretty simple!
Thank you, anyway!
September 28th, 2010 at 12:39 pm
Hi
I want to know how to add custom field for attribute option? For example
if attribute name is “Carat” for diamond product
Its value can be 1, 2, 3, 4, 5
I can Add up to this level in magento, with the help of attribute and its option but I want extra field called “description” which explain what is the meaning of value 1 (carat=1) and what is the mean of 2, admin will write some description about it. So admin will enter data like
English English_Desc French French_Desc
1 This is eng des for 1 1 This is french desc 1
Thanks
John
September 28th, 2010 at 1:58 pm
@ John: I am not sure that I follow. Do you want the description to display with the value?
Or is the description only for the admin?
October 16th, 2010 at 9:26 pm
Hi, thank for that post it’s so usefull, but still i’ve got some doubts
i’ve got a multi-select attribute added on default set, and i want to retrieve in frontend those attributes who are selected for that product from backend, i’m trying to get that attribute when you click on cart option, so when you click addtocart on View.php and i was trying all day to do it, and i have no lucky.
that my code:
$attributes = $this->getProduct()->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
if($a->getAttributeCode() == 'machine_opt'){
foreach($a->getSource()->getAllOptions() as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
}
October 17th, 2010 at 8:16 am
@Manuel: Try the new code I added:
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product','machine_opt');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
October 19th, 2010 at 3:07 pm
Hey Mike D,
Just noticed a potentially confusing typo in your first block of code:
$attrubuteArrayspelt with a ‘u’(Well, not really a typo because you can call the array whatever you like but you know what I mean)
It’s in the first block of code:
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attrubuteArray[$option['value']] = $option['label'];
}
Good article anyhow, found it very useful.
Thanks!
November 8th, 2010 at 11:27 am
You made my day! Thanks a bunch!
November 24th, 2010 at 5:25 pm
Hi Mike, thanks a lot for the article, its very handy and helpful. I was wondering, do you know how can the options for a particular attribute be removed? For instance if I have the attribute color and I want to delete the option ‘red’.
Thanks in advance.
November 24th, 2010 at 6:26 pm
Goto manage attributes in the backend and edit the desired attribute. Click on the second tab on the left and that should bring up a page that lists the options for that attribute. Now just delete the option you don’t want.
December 23rd, 2010 at 11:44 am
Oh! Thank you very much !
January 13th, 2011 at 4:17 am
[...] Per ottenere tutte le caratteristiche relative ad un attributo potete fare riferimento a questo blog: Magento: Getting product attributes values and labels [...]
January 17th, 2011 at 9:04 am
nice work dude…. ^^<
January 22nd, 2011 at 3:33 am
Some times like in grouped.phtml you have to do it like this
htmlEscape($_item->getAttributeText(‘contenance’)); ?>
February 23rd, 2011 at 12:12 am
How can I make my attribute a link? I am using this to show the actual attribute value, but is there a way to get the URL?
echo $_product->getResource()->getAttribute(‘manufacturer’)->getFrontend()->getValue($_product);
February 23rd, 2011 at 8:00 am
I am not exactly sure what you are asking for. The attribute does not have a link. It is just an attribute. Are you asking how to make it a link to a search results page or something?
February 23rd, 2011 at 8:55 am
The easiest way would be to just use a switch in your code and just hard code the link you want. Other wise you will need to determine what category it needs to redirect to then use the layered navigation model to create the url based on the category and attribute. And for a beginner that would probably be an under taking. It would also be different based on where on you site you are putting the link.
March 7th, 2011 at 5:27 pm
I used the first code to pull simple options. I am on Magento 1.5 and it displays ADMIN instead of the Default Store View value. How can I get it to display the Default Store View??
Chris
March 7th, 2011 at 5:35 pm
Figured it out, just had to turn true to false like so: $attribute->getSource()->getAllOptions(false)
March 8th, 2011 at 2:10 pm
How do you get only attributes that have been assigned to products?
March 8th, 2011 at 2:49 pm
Hey Chris,
That is a little trickier. Once I get home I can post some code that I have used to get the attributes, but it is not pretty
March 9th, 2011 at 4:11 pm
Were you able to find your code?
March 10th, 2011 at 9:43 am
Hey Chris,
I have updated the post to have an example of pulling attribute values for just values used on products. See the last example. Hope it helps.
March 10th, 2011 at 11:14 am
Thanks so much!
March 22nd, 2011 at 1:41 pm
Hi,
I have a problem to blank a attribute on the produkt overview. Normally it should work fine with “Used in Product Listing=NO” in attribute manage page.
But for us, it only works for “drop-down-attributes”, nut fpr “text-attributes” for example.
When I choose “Used in Product Listing=NO” für a “text-attribute”, it only blanks out the value of the attribute, not the label.
Do you have an idea for this?
Thank you very much and sorry for my bad english!
getAttributes(); ?>
getIsVisibleOnFront()) : ?>__($attribute->getFrontend()->getValue($_product)) !="Nein"):?>
__($attribute->getFrontend()->getLabel($_product)) ;?>: __($attribute->getFrontend()->getValue($_product)) ;?>
best regards, Dan
March 22nd, 2011 at 1:42 pm
getAttributes(); ?>
getIsVisibleOnFront()) : ?>__($attribute->getFrontend()->getValue($_product)) !=”Nein”):?>
__($attribute->getFrontend()->getLabel($_product)) ;?>: __($attribute->getFrontend()->getValue($_product)) ;?>
March 23rd, 2011 at 8:12 am
Hey Dan,
Try Wrapping you code in the following tags “
“, that should allow it to show up properly in the comment box.Then I should be able to help you better.
Thanks.
March 23rd, 2011 at 8:28 am
Hi Mike,
Thank you for your advice.
I tried it with the tag, but it didn’t work.
But we found a solution for our problem. The reason why it only blanks out the value of the attribute, not the label in the product overview was:
“Used for Sorting in Product Listing” must be “NO”. Then it works, I don’t know why and I don’t understand the coherence, but it works.
April 7th, 2011 at 7:15 pm
Hello from Kiev
Thank you for great post!
This line
//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)
can fatal if product not have ‘club_type’ attribute, maybe better
if($attribute = $_product->getResource()->getAttribute('club_type')) {
$attributeValue = $attribute->getFrontend()->getValue($_product);
}
June 17th, 2011 at 3:39 pm
Your first code snippet worked perfectly. I was trying to get all of the colors used by all products in our Magento storefront. I had looked around here and there for a few days and your example was the first one that worked. Thanks so, so much for posting this.
June 25th, 2011 at 9:48 am
[...] http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/ [...]
August 11th, 2011 at 8:49 am
Thanks so much for the post. It saved me. Much appreciated!
-Chris
August 20th, 2011 at 8:27 am
Nice solution, I have tried http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/get_manufacturers_and_any_attribute_list
Thanks!
August 31st, 2011 at 8:35 am
Thanks a lot Mike.
It helps me a lot to get the attribute labels and values of the drop down type.
October 25th, 2011 at 10:59 am
Thanks! This post helped a lot
November 1st, 2011 at 3:57 am
I found good link on getting product attributes values and labels. It may help you.
http://www.phptechi.com/getting-product-attributes-values-and-labels.html
December 11th, 2011 at 11:38 am
Thanks it works. I used it for special_to_date
December 12th, 2011 at 9:11 am
Hi there,
I am trying to retrieve attributes and all its options for Customers in Magento. This is my code
$attributeIds = Mage::getStoreConfig('customerattributes_options/list/attributes');
$attributes = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_id', array('in' => $attributeIds));
foreach ($attributes as $attribute)
{
echo 'Code: '. $attribute->getId();
echo 'Index: ' . $attribute->getCode();
echo 'Header: ' . $attribute->getFrontendLabel();
echo 'Type: ' . $attribute->getFrontendInput();
echo ' Options: ' . $attribute->getSource()->getAllOptions(false);
}
But it throws an Error Exception and doesn’t show me any options for Attributes like Gender.
Does anyone know what’s the error in it?
December 12th, 2011 at 9:22 am
@ magento_20,
You need to provide a little more info.
1. What is the error that is thrown?
2. What is the sql that is run?
Get the sql by using the following:
//Put this just above your foreach
print_r($attributes->getSelect()->__toString());die("Sql");
December 12th, 2011 at 9:29 am
@Mike D
Thanks for your reply. this is the error,
Fatal error: Uncaught exception ‘Mage_Eav_Exception’ with message ‘Source model “” not found for attribute “fax”‘ in /home/wpfsl/public_html/app/Mage.php:549 Stack trace: #0 /home/wpfsl/public_html/app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php(387): Mage::exception(‘Mage_Eav’, ‘Source model “”…’) #1 /home/wpfsl/public_html/index.php(91): Mage_Eav_Model_Entity_Attribute_Abstract->getSource() #2 {main} thrown in /home/wpfsl/public_html/app/Mage.php on line 549
And your code doesn’t print out the sql query, I’m afraid.
December 14th, 2011 at 11:56 am
You may need to load each attribute before being able to get the options.
Here is what I would do:
1. Comment out the line where you are getting the options. I think that is what is throwing the error.
2. See if you are actually getting date out of $attribute
A) What class is the $attribute object?
December 15th, 2011 at 9:44 am
Hi,
I used this code source to load all product attribute names and to fill multisellect component:
$attributeArray=array();
$attributesInfo = Mage::getResourceModel(‘eav/entity_attribute_collection’)
->setEntityTypeFilter(4)
->addSetInfo()
->getData();
foreach ($attributesInfo as $a){
$attributeArray[] = $a['frontend_label'];
}
return $attributeArray;
The problem is that I got just the first letters of frontend_label and all of them are big letters. What is wrong in my source code or I have to change something other?
Many thanks in advance!
December 15th, 2011 at 11:01 am
@magento_mk:
1. What is contained in $a?
2. You may need to tell the collection to load other data. Look at the collection model and see what the other options are.
January 3rd, 2012 at 2:53 pm
Fatal error: Uncaught exception ‘Mage_Eav_Exception’ with message ‘Source model “” not found for attribute “fax”‘
This can occur if you have created the attribute but forgot to assign it to the attribute set of the product you are creating.