Google+

Pages

Monday, July 30, 2012

Hide Attributes which have no value in Magento

If you don't provide a value of attribute for a product in Magento, it displays as 'No' rather than hiding that attribute. This means attributes that aren't relevant for a product will still appear confusing the user. Which is a bad impression of your website.

To hide these unnecessary attributes, do the following:


Open attribute.phtml, which is located in app/design/frontend/[your package]/[your theme]/template/catalog/product/view folder, and find code like this:



<?php foreach ($_additional as $_data): ?>
    <tr>
        <th><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
        <td><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
    </tr>
<?php endforeach; ?>


Now replace this by:



<?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
    if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != ")) { ?>
        <tr>
            <th><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
            <td><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
        </tr>
    <?php } ?>
<?php endforeach; ?>


and you are done.. enjoy.. :)


No comments: