Knowledge Base

How to Hide Empty Attributes on Product Pages in Magento

Depending on how you set your attributes up, you may find that not every attribute in an attribute set applies to every product it is assigned to. The empty attributes will still show in the Additional tab on your product pages, but they will show with a value of No or N/A.

Empty attributes

To hide the empty attributes for a product, change the following:

Find and open the attributes.phtml file. This file can be found here: /app/design/frontend/[YOUR PACKAGE]/[YOUR THEME]/template/catalog/product/view/attributes.phtml. If the attributes.phtml file did not come with your theme, be you can find it in /app/design/frontend/base/default/template/catalog/product/view/attributes.phtml. BE SURE TO CREATE A COPY OF THIS FILE AND ADD IT TO YOUR THEME, DO NOT EDIT THIS CORE FILE.

Open the file and search for the following lines:

<?php foreach ($_additional as $_data): ?>
    <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 endforeach; ?>

Replace the entire foreach loop with the following lines of code:

<?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 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 endforeach; ?>

Attributes

The attributes with nothing selected will no longer show.

Source: http://codingbasics.net/hide-magento-attributes-value/

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *