Magento Get Value Product Attribute

How to get a specific product attribute value if I know the product identifier without downloading the entire product?

+79
attributes product magento
Aug 03 2018-11-11T00:
source share
11 answers
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId); 
+129
Nov 17 '11 at 9:40
source share

The way I know:

 $product->getResource()->getAttribute($attribute_code) ->getFrontend()->getValue($product) 
+36
Aug 03 2018-11-12T00:
source share

you can use

 <?php echo $product->getAttributeText('attr_id') ?> 
+26
Jul 10 '13 at 7:09
source share

It seems impossible to get value without loading the product model. If you look at the file app / code / core / Mage / Eav / Model / Entity / Attribute / Frontend / Abstract.php, you will see a method

 public function getValue(Varien_Object $object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); if (in_array($this->getConfigField('input'), array('select','boolean'))) { $valueOption = $this->getOption($value); if (!$valueOption) { $opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean(); if ($options = $opt->getAllOptions()) { foreach ($options as $option) { if ($option['value'] == $value) { $valueOption = $option['label']; } } } } $value = $valueOption; } elseif ($this->getConfigField('input')=='multiselect') { $value = $this->getOption($value); if (is_array($value)) { $value = implode(', ', $value); } } return $value; } 

As you can see, this method requires a loaded object to get data from it (3rd line).

+7
Aug 03 '11 at 12:15
source share

Please see Daniel Kocherga's answer as it will work for you in most cases.

In addition to this method, to get the value of an attribute, you can sometimes get a select or multiselect . In this case, I created this method, which I store in a helper class:

 /** * @param int $entityId * @param int|string|array $attribute atrribute ids or codes * @param null|int|Mage_Core_Model_Store $store * * @return bool|null|string * @throws Mage_Core_Exception */ public function getAttributeRawLabel($entityId, $attribute, $store=null) { if (!$store) { $store = Mage::app()->getStore(); } $value = (string)Mage::getResourceModel('catalog/product')->getAttributeRawValue($entityId, $attribute, $store); if (!empty($value)) { return Mage::getModel('catalog/product')->getResource()->getAttribute($attribute)->getSource()->getOptionText($value); } return null; } 
+7
May 29 '15 at 1:32
source share

First, we need to make sure that the desired attribute is loaded, and then output it. Use this:

 $product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>')); $attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product); 
+5
Nov 20 '13 at 16:31
source share

try it

  $attribute = $_product->getResource()->getAttribute('custom_attribute_code'); if ($attribute) { echo $attribute_value = $attribute ->getFrontend()->getValue($_product); } 
+2
Apr 03 '15 at 10:22
source share

You do not need to download the entire product. Magentos collections are very powerful and smart.

 $collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('entity_id', $product->getId()); $collection->addAttributeToSelect('manufacturer'); $product = $collection->getFirstItem(); $manufacturer = $product->getAttributeText('manufacturer'); 

At that moment when you call getFirstItem (), the request will be executed and the result of the product will be minimal:

 [status] => 1 [entity_id] => 38901 [type_id] => configurable [attribute_set_id] => 9 [manufacturer] => 492 [manufacturer_value] => JETTE [is_salable] => 1 [stock_item (Varien_Object)] => Array ( [is_in_stock] => 1 ) 
+1
Jan 31 '14 at 11:00
source share

This one works -

 echo $_product->getData('ATTRIBUTE_NAME_HERE'); 
0
Apr 13 '16 at 5:51 on
source share

You can write a method that will do this directly through sql. I suppose.

It will look something like this:

Variables

 $store_id = 1; $product_id = 1234; $attribute_code = 'manufacturer'; 

Query:

 SELECT value FROM eav_attribute_option_value WHERE option_id IN ( SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET( option_id, (SELECT value FROM catalog_product_entity_varchar WHERE entity_id = '$product_id' AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code='$attribute_code') ) ) > 0) AND store_id='$store_id'; 

You will need to get the value from the correct table based on the backend_type attribute (field in eav_attribute), although this requires at least 1 additional query.

-2
Aug 03 2018-11-12T00:
source share

If you have a text / textarea attribute named my_attr, you can get it: product->getMyAttr();

-2
May 25 '15 at 19:49
source share



All Articles