6

I have manage to retrieve the value for an attribute relative to a particular product

 $store_id = Mage::app()->getStore()->getStoreId(); $attribute_code = 'code'; $value = Mage::getResourceModel('catalog/product') ->getAttributeRawValue($prod_id, $attribute_code, $store_id); 

What I miss here is the label associated to the attribute value.

How can I get it, without load the full product object ?

5 Answers 5

15

This code works fine and avoid to load the entire product obj

$option_id = Mage::getResourceModel('catalog/product') ->getAttributeRawValue($prod_id, $attribure_code, $store_id); 

Method 1

$option_value = Mage::getResourceModel('eav/entity_attribute_option_collection') ->setStoreFilter($store_id) ->setIdFilter($option_id) ->getFirstItem() ->getValue(); 

Method 2 (@Marius)

// to be tested // not loading the product - just creating a simple instance $singleton_prod = Mage::getSingleton('catalog/product') ->setStoreId($store_id) ->setData($attribure_code, $option_id); $optionLabel = $singleton_prod->getAttributeText($attribure_code); 
4
  • Regarding Method2 , it should be $_product = Mage::getModel('catalog/product')->setStoreId($storeId)->setYourAttributeCode($yourAttributeOptionId); $productOptionLabel = $_product->getAttributeText($yourAtrributeCode); Credits to @Marius - magento.stackexchange.com/a/8396/695 Commented Mar 23, 2018 at 11:25
  • I don't see the difference in the code you posted. setYourAttributeCode($value) or setData($attribute_code, value) is the same ( credit to varien team :) Commented Mar 23, 2018 at 13:33
  • setData doesn't work , whereas setAttributeCode works , atleast in my case ...(verified with older 1.6.1.x version and in the recent 1.9.3.x. version), though the difference is not visible. Commented Mar 23, 2018 at 14:14
  • this sound very strange to me, I test it when I have some time and updated the answer if required, thanks Commented Mar 27, 2018 at 5:37
7

You can also use some thing like this if you have $product variable.

$product->getResource()->getAttribute($attribute_code)->getFrontend()->getLabel($product); 
4
  • The only problem here is the OP already has the resource model. Commented Apr 15, 2013 at 13:29
  • Yes, i just suggest other method to retrieve label, if op has product_id then may be he has product object Commented Apr 15, 2013 at 13:32
  • I don't want to load the full prod model only for getting the label if possible ... Commented Apr 15, 2013 at 14:16
  • 1
    getAttributeRawValue only gives you value of particular attribute Commented Apr 16, 2013 at 4:36
4

I used this code.

$some_attr_code = "brand" $optionId = Mage::app()->getRequest()->getParam('option_id'); /** @var Mage_Eav_Model_Attribute $attribute */ $attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $some_attr_code); $label = $attribute->getStoreLabel(); if($optionId){ $optionLabel = $attribute->getFrontend()->getOption($optionId); } 
1

This solution isn't bound to a strict model or a resource model and can be used to retrieve an option label of an attribute that is assigned to whatever entity type:

// In your case $entityType should be `catalog_product` $attribute = Mage::getModel('eav/config')->getAttribute($entityType, $attributeCode); echo $attribute->getSource()->getOptionText($optionId); 

FYI, $attribute->getSource() returns an instance of Mage_Eav_Model_Entity_Attribute_Source_Table which has the following methods:

Array ( [0] => getAllOptions [1] => getOptionText [2] => addValueSortToCollection [3] => getFlatColums [4] => getFlatIndexes [5] => getFlatUpdateSelect [6] => setAttribute [7] => getAttribute [8] => getOptionId [9] => getIndexOptionText ) 

I should note that Method 1 from accepted answer throws an SQL error in Magento v1.9.4 (I'm not sure about the other versions):

Column 'option_id' in where clause is ambiguous 

The reason could be found in the below generated SQL query:

SELECT `main_table`.*, `tdv`.`value` AS `default_value`, `tsv`.`value` AS `store_default_value`, IF ( tsv.value_id > 0, tsv.VALUE, tdv.VALUE ) AS `value` FROM `eav_attribute_option` AS `main_table` INNER JOIN `eav_attribute_option_value` AS `tdv` ON tdv.option_id = main_table.option_id LEFT JOIN `eav_attribute_option_value` AS `tsv` ON tsv.option_id = main_table.option_id AND tsv.store_id = 0 WHERE ( tdv.store_id = 0 ) # This is the culprit AND ( `option_id` IN ( 9999 ) ) 
1
  • This is an excellent explanation. Commented Apr 9, 2022 at 11:27
0

What about this?

$attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute_code); $label = $attribute->getFrontend()->getLabel(); 
4
  • Sorry but this is not what I'm asking ... the value of the attribute is related to a particular product/store id ... Commented Apr 15, 2013 at 14:18
  • I'm sorry, but it really wasn't clear from your question... Commented Apr 15, 2013 at 15:23
  • Anyway, in attribute model Mage_Eav_Model_Entity_Attribute you also have the method getStoreLabel($storeId = null) Commented Apr 15, 2013 at 15:41
  • ... "for an attribute relative to a particular product" :) Commented Apr 15, 2013 at 15:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.