28Jan/1126

Empty Attributes Showing as “No” or “N/A”? Here’s a Quick Fix!

The Problem

For some odd reason, the Magento developers decided that an empty attribute should NOT be empty, but rather "No" or "N/A", depending on its type. This is not just annoying, but in some cases can display wrong information which means confused visitors and potentially lost sales.

The Fix

Fortunately, there is a quick fix that will solve the problem until the Magento developers fix this bug. Open the file /app/design/frontend/default/[theme name]/template/catalog/product/view/attribute.phtml in an editor and find 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 these lines with this:

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

Save the file, upload it to your server and refresh the cache. Your "empty" attributes will now be hidden!

About Dave Haefliger

Dave “2.0” Haefliger is a simple and effective solutions kind of guy. He loves building applications that feel intuitive. Dave has an unnatural love for almost everything that comes from Apple. Most people don’t understand it, but he doesn’t care, really.
Comments (26) Trackbacks (0)
  1. Exact path would be : /app/design/frontend/default/[theme name]/CATALOG/template/product/view/attribute.phtml
    Thanks for the fix! :)

  2. thx a lot! nice fix

  3. If you are using Template masters easytabs extension you are going to find the attribute file in the extension folder in the design folder.

  4. great, good fix! Thank you!

  5. Thanks for this fix. Never thought that there would be so much trouble updating Magento to 1.4.2…

  6. Amazing! Thank you so much.

  7. Thanks! This fixed an annoying little quirk.

  8. I’m using the Magento Classic theme (f002) and there is no such file (Magento version 1.5.0.1). Any idea what to do? Thanks!

  9. Thx alot, works fine 4 me

  10. daghang salamat…it works!

  11. We use the version 1.5.0.1 and i can’t find the file attribute.phtml.

    Anyone has fix it in the version 1.5.0.1?

  12. If you do not find the file in your theme, then look for it here:

    /app/design/frontend/default/default/template/catalog/product/view/attributes.phtml

    or if that fails, go here:

    /app/design/frontend/base/default/template/catalog/product/view/attributes.phtml

  13. Thanks for the fix, resolved an annoying problem (ver 1.5.0.1).

  14. I tried to find this file but I think I don’t have anything like this…
    the only file is media.phtml

    Using magento (ver 1.5.0.1)

    • If you do not find the file in your theme, then look for it here:
      /app/design/frontend/default/default/template/catalog/product/view/attributes.phtml
      or if that fails, go here:
      /app/design/frontend/base/default/template/catalog/product/view/attributes.phtml

  15. Thanks so much for the post!!! Great Fix!

  16. Do you have a similar solution for the product Compare Products page?

    It is still displaying “No” for my blank attributes on the product comparison page. I tried copying and pasting your if statement into app/design/frontend/default/[my theme]/template/catalog/product/compare/list.phtml (around line 83), but it didn’t work.

    Any help would be appreciated.

    Using Magento 1.5.0.1

    • Matt,

      use the following code to remove the empty attributes on the product compare page. (using app/design/frontend/default/[my theme]/template/catalog/product/compare/list.phtml) On line 86, add the following:
      if (!is_null($_item->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_item) != '')):

      It should look like so:

      < ?php foreach($this->getItems() as $_item):
      if (!is_null($_item->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_item) != '')): ?>
      < ?php if($_i++%10==0): ?>



      < ?php endif; ?>
      < ?php endforeach; ?>

      • Thanks Dave!!! It worked like a charm.

        Hey, just in case anybody has trouble figuring out where to put these snippets of code, here is the whole tag from line 85 of app/design/frontend/default/[my theme]/template/catalog/product/compare/list.phtml. (I’m probably not the only one out there that just wants a whole block of code from a forum when I’m working late at night or against a tight deadline, so hopefully, this will make someone’s day…)

        <?php foreach($this->getItems() as $_item): ?>
        <?php /* begin if empty attribute check */ ?>
        <?php if (!is_null($_item->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_item) != ”)): ?>
        <?php if($_i++%10==0): ?>
        <th><span class="nobr"><?php echo $_attribute->getStoreLabel() ?></span></th>
        <?php endif; ?>
        <td>
        <?php switch ($_attribute->getAttributeCode()) {
        case "price": ?>
        <?php echo $this->getPriceHtml($_item, true, ‘-compare-list-’ . $_attribute->getCode()) ?>
        <?php break;
        case "small_image": ?>
        <img src="<?php echo $this->helper(‘catalog/image’)->init($_item, ‘small_image’)->resize(125, 125); ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" />
        <?php break;
        case "date":
        echo substr($this->getProductAttributeValue($_item, $_attribute),0,10);
        break;
        default: ?>
        <?php echo $this->helper(‘catalog/output’)->productAttribute($_item, $this->getProductAttributeValue($_item, $_attribute), $_attribute->getCode()) ?>
        <?php break;
        } ?>
        </td>
        <?php /* end if empty attribute check */ ?>
        <?php endif; ?>
        <?php endforeach; ?>

  17. Or you could simply try this in the Attributes.php

    Instead of :
    elseif ((string)$value == ”) {
    $value = Mage::helper(‘catalog’)->__(‘No’);

    Place this:
    elseif ((string)$value == ”) {
    //$value = Mage::helper(‘catalog’)->__(‘No’); Comment out this line of code and add continue;.
    continue;

  18. Just want to say – your solution ROCKS! :-) Thank you!

  19. Exactly what I needed, thanks! Note to those copying MattBill’s code, be sure to replace the smart quotes with regular quotes (‘). Works as pasted otherwise.

  20. Super! worked first time, saved much time!

  21. Many Thanks, that realy help saved me many time. It worked with magento 1.5.10

  22. Supa Thank to you !

    Your snippet is really usefull, I use it on my product page, compare page, I even customize it to only display “no empty” attributs and grouped them by their attribut groupe from Attributs Set.

    Big thank from French riviera !


Leave a comment


No trackbacks yet.

Categories

Tag Cloud

Random Posts

Archives

Meta