Is there an easier way to get the value of the frontend attribute?

I have an array of attribute codes that I need to get the values:

$attributes = array( 'Category' => 'type', 'Manufacturer' => 'brand', 'Title' => 'meta_title', 'Description' => 'description', 'Product Link' => 'url_path', 'Price' => 'price', 'Product-image link' => 'image', 'SKU' => 'sku', 'Stock' => 'qty', 'Condition' => 'condition', 'Shipping cost' => 'delivery_cost'); 

After iterating through the product collection, I get the interface attribute values ​​as follows:

 $attributeId = Mage::getResourceModel('eav/entity_attribute') ->getIdByCode('catalog_product', $attribute_code); $attribute = Mage::getModel('catalog/resource_eav_attribute') ->load($attributeId); $value = $attribute->getFrontend()->getValue($product); 

Just using $product->getDate($attribute) will not work with drop-down lists and multiple selections, it just returns their identifier and not its frontend value.

Although the above code works, it seems to be of great importance in getting the value, but more importantly, it works quite slowly. Is there a faster / smarter way to get interface values ​​for product attributes?

Edit
Now I have the following (after considering special cases such as image and qty ), which is slightly easier on the eyes and seems to work faster (although I don’t know why):

 $inputType = $product->getResource() ->getAttribute($attribute_code) ->getFrontend() ->getInputType(); switch ($inputType) { case 'multiselect': case 'select': case 'dropdown': $value = $product->getAttributeText($attribute_code); if (is_array($value)) { $value = implode(', ', $value); } break; default: $value = $product->getData($attribute_code); break; } $attributesRow[] = $value; 

If someone can improve this (make it simpler / more efficient), send a response.

+7
source share
3 answers

For drop-down menus and multi-selects and only with products (this is not a common EAV trick) you can use getAttributeText() .

 $value = $product->getAttributeText($attribute_code); 
+11
source

In version 1.7, $product->getAttributeText($attribute_code) for me does not work on the product page. At first I thought that this was because the attribute was not specified in the catalog_product_flat index. But it turned out that the attribute was there. In any case, the following code works for me. I try a simple code and then return to the EAV code.

So, I use this code:

 $value = $product->getAttributeText($attribute_code); // first try the flat table? if(empty($value) ) { // use the EAV tables only if the flat table doesn't work $value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product); } 
+3
source

It depends on how you configured the attribute (is it accessible from the context you are trying to reach?), But the easiest way is usually this (for meta_title, for example):

 $product->getMetaTitle() 
0
source

All Articles