Convert an attribute to a link

In the “additional information” section of the product view page, I had removed the “N/A” attributes (see post) but then I decided that it would be nice to have a few of the additional attributes such as manufacturer and classification as links to the product search page to help customers search.

The change is pretty straight-forward and you can change the attribute names or linked page to what’s appropriate for your needs. Where the search was looking at these attributes anyway, no other changes were needed. (Note: if you’re going to use the search page, make sure the attributes you select are included as part of the search – modify the attributes in the admin section.

OPEN: /app/design/frontend/default/YOURTHEME/template/catalog/product/view/attribute.phtml

Make the changes as shown below (these mods already include the ‘ignore N/A and empty attributes’)


<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if(!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<?php if(preg_match("/classification|manufacturer/i",($this->htmlEscape($this->__($_data['label']))))) { ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<?php $query = str_replace(' ', '+', trim($_helper->productAttribute($_product, $_data['value'], $_data['code']))); $query = str_replace('&amp;','%26', $query); ?>
<?php $base_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>
<td class="data"><?php echo "<a href=\"" . $base_url . "catalogsearch/result/?q=" . $query . "\">" . $_helper->productAttribute($_product, $_data['value'], $_data['code']) . "</a>"; ?></td>
</tr>
<?php } else { ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php } ?>
<?php endforeach; ?>

When you bring up the product view page, you should notice that the select attributes will now be clickable links.

additional notes:

If you’re having trouble with ampersands in your search query, modify the first section so that it looks like this:


 

<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<?php $query = str_replace(' ', '+', trim($_helper->productAttribute($_product, $_data['value'], $_data['code']))); $query = str_replace('&amp;','%26', $query); ?>
<td class="data"><?php echo "<a href=\"catalogsearch/result/?q=" . $query . "\">" . $_helper->productAttribute($_product, $_data['value'], $_data['code']) . "</a>"; ?></td>
</tr>

 

Leave a Reply