Magento - Show Custom Attributes in Grouped Product table


Question

I need to find a way to show the value of a custom attribute in place of the "Product Name" shown in the image below.

Grouped products table
(source: magentocommerce.com)

I'm working with /app/design/frontend/default/defaultx/template/catalog/product/view/type/grouped.php

The code below doesn't work(the custom attribute is yearmade):

<?php if (count($_associatedProducts)): ?>  
<?php foreach ($_associatedProducts as $_item): ?>  
    <tr>  
        <td><?php echo $this->htmlEscape($_item->getYearmade()) ?></td>

Any help would be appreciated.

EDIT: So the answer turned out to be quite simple. You see what I failed to mention above was that there was indeed output... but that it was just a number (eg: 52). Turns out this was the ID for that custom attribute value (It was a Dropdown type of custom attribute).

So in summary
This works for custom attributes of type text:

echo $this->htmlEscape($_item->getYearmade())

But for all other types of custom attribute (I think), the following should be used:

echo $this->htmlEscape($_item->getAttributeText('yearmade'))

I would not have discovered this without the most excellent answer provided by Alan Storm, below. Thank you sir.

1
5
3/23/2019 5:59:57 AM

Accepted Answer

All Magento models have a "getData" method available, which will return an php-array of key/value pairs. Try this at the top of your grouped.phtml file (after $_product is defined)

print('<pre>');print_r($_product->getData());print('</pre>');

You should see output that looks something like the following.

Array
(
    [store_id] => 1
    [entity_id] => 3437
    [entity_type_id] => 4
    [attribute_set_id] => 27
    [type_id] => grouped
    [sku] => 
    [category_ids] => 
    [created_at] => 2009-04-16 03:37:51
...     

So, you can grab an array of properties and just pull the key out. You could also use Magento's convenience/magic getX and setX methods. On all Magento models, you can access any property in the data array by calling a method based on the camel case version of the name,

$created_at = $_product->getCreatedAt();
$_product->setCreatedAt($date);

So, whatever your custom attribute name is, you should be able to get at it using the above, and if you're not sure just print_r or var_dump the contents of the array returned by getData().

Finally, if the custom attribute is on one of the related products simple product, you'll wants something more like

$_associatedProducts[0]->getCreatedAt();
3
6/17/2009 6:58:21 AM

I had the same problem.

  1. You must locate grouped.phtml app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml

  2. Get the item, example $_item[units]

  3. Add a cell into the table and paste echo $_item['units'];

  4. Thats all :)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon