Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a simpler way to get an attribute's frontend value?
    primarykey
    data
    text
    <p>I have an array of attribute codes which I need to get the values of:</p> <pre><code>$attributes = array( 'Category' =&gt; 'type', 'Manufacturer' =&gt; 'brand', 'Title' =&gt; 'meta_title', 'Description' =&gt; 'description', 'Product Link' =&gt; 'url_path', 'Price' =&gt; 'price', 'Product-image link' =&gt; 'image', 'SKU' =&gt; 'sku', 'Stock' =&gt; 'qty', 'Condition' =&gt; 'condition', 'Shipping cost' =&gt; 'delivery_cost'); </code></pre> <p>After iterating through a product collection I get the frontend values of the attributes like so:</p> <pre><code>$attributeId = Mage::getResourceModel('eav/entity_attribute') -&gt;getIdByCode('catalog_product', $attribute_code); $attribute = Mage::getModel('catalog/resource_eav_attribute') -&gt;load($attributeId); $value = $attribute-&gt;getFrontend()-&gt;getValue($product); </code></pre> <p>Simply using <code>$product-&gt;getDate($attribute)</code> won't work with dropdowns and multi-selects, it just returns their id and not their frontend value.</p> <p>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?</p> <p><strong>Edit</strong><br> I now have the following (after dealing with special cases like <code>image</code> and <code>qty</code>) which is a bit easier on the eyes and does seem to run quicker (although I don't know why):</p> <pre><code>$inputType = $product-&gt;getResource() -&gt;getAttribute($attribute_code) -&gt;getFrontend() -&gt;getInputType(); switch ($inputType) { case 'multiselect': case 'select': case 'dropdown': $value = $product-&gt;getAttributeText($attribute_code); if (is_array($value)) { $value = implode(', ', $value); } break; default: $value = $product-&gt;getData($attribute_code); break; } $attributesRow[] = $value; </code></pre> <p>If anyone can improve this (make it simpler/more efficient), please post an answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload