Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simpler way to get an attribute's frontend value?

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

$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 a product collection I get the frontend values of the attributes like so:

$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);

Simply using $product->getDate($attribute) won't work with dropdowns and multi-selects, it just returns their id and not their frontend value.

While the code above works, it seems to be a long way around getting the value, but more importantly it runs quite slow. Is there a faster/more sensible way to get the frontend values for product attributes?

Edit
I now have the following (after dealing with special cases like image and qty) which is a bit easier on the eyes and does seem to run quicker (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 anyone can improve this (make it simpler/more efficient), please post an answer.

like image 884
Jamie Avatar asked Aug 18 '11 10:08

Jamie


2 Answers

For dropdowns and multiselects and only with products (this isn't a general EAV trick) you can use getAttributeText().

$value = $product->getAttributeText($attribute_code);
like image 194
clockworkgeek Avatar answered Nov 07 '22 04:11

clockworkgeek


In version 1.7, $product->getAttributeText($attribute_code) is not working for me on the product page. At first I thought it was because the attribute was not 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 the simple code, then fall back on the EAV code.

So I am using code like this:

$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);
}
like image 39
Buttle Butkus Avatar answered Nov 07 '22 06:11

Buttle Butkus