Sometimes, you need to get a precise information about a product, and it’s very boring to load it.
Two methods are possible :
Collection method
$productId = 25; // Your product ID $model = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('attribute_code') ->addAttributeToFilter('entity_id',$productId) ->getFirstItem(); $attribute_value = $model->getAttributeCode();
We retrieve one result with the “getFirstItem” method, and get the attribute value thanks to “addAttributeToSelect” method
AttributeRawValue Method
With a single line of code, you can get the attribute value of your product :
$productId = 25; $attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code');
You can add a third parameter which is the storeId or the store object itself.
$productId = 25; $attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', 1);
If I want many values?
If you want many values, in the collection method, you have to put an array in your addAttributeToSelect :
$model = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect(array('attribute_code','attribute_code_bis')) ->addAttributeToFilter('entity_id',$productId) ->getFirstItem(); $attribute_value = $model->getAttributeCode(); $attribute_value_bis = $model->getAttributeCodeBis();
And with AttributeRawValue method, you need one line per attribute :
$rc = Mage::getResourceModel('catalog/product'); $attribute_value = $rc->getAttributeRawValue($productId, 'attribute_code'); $attribute_value_bis = $rc->getAttributeRawValue($productId, 'attribute_code_bis');
Which is the best?
Performances between the two methods are close, and better than a load. If you method will be called many times, “getAttributeRawValue” is better. If it will be called only one time during the page load, prefer “getCollection”.
There is a complete article on this website :
How to get product’s attribute with getAttributeRawValue() in Magento (Get product attribute without load on Magento)
Retrieve attribute’s label
When my attribute is a select, how to retrieve the real value ?
With the 2 methods above, you will have the “value_id” of the value. But you can need to have the label of this attribute value :
// Not loading the product, just creating one simple instance $product = Mage::getModel('catalog/product') ->setStoreId($store_id) // Set a store ID to define the language, 0 for Admin ->setAttributeCode($attribute_value); // Respect Camel case for your setter $attribute_label= $product->getAttributeText('attribute_code');
If you already have a product instance :
// Not loading the product, just creating one simple instance $attribute_label = $product ->setStoreId($store_id) // Set a store ID to define the language, 0 for Admin ->setAttributeCode($attribute_value) // Respect Camel case for your setter ->getAttributeText('attribute_code');
Retrieve attribute option label in SQL (with store_id)
Here you can find a little function with 2 parameters : the id of the value and the store id (optional).
You can put it on a helper for example.
public function getAttributeOptionValue($optionId, $storeId = 0){ $db = Mage::getSingleton('core/resource')->getConnection('core_read'); $query = 'SELECT value FROM eav_attribute_option_value WHERE (store_id = '.$storeId.' OR store_id = 0) AND option_id = '.$optionId.' ORDER BY store_id DESC LIMIT 0,1'; try{ $row = $db->fetchRow($query); } catch(Exception $e){ return false; } if(empty($row) || !isset($row['value'])){ return false; } return $row['value']; }
Thanks, this was really helpful!