Magento – How to hide empty attribute in any magento template

ce-1.8.1.0product-attribute

I want to hide the custom attribute in magento template. My magento version is 1.8.1

We have added custom attribute for our product like brand, dimension, product type etc. but sometimes we didn't add values in these attributes. magento shows No or N/A in product view page.

So, we want to hide attributes which are empty or no value in template.

Best Answer

A quick fix:

In app/[mypackage]/[mytheme]/template/catalog/product/view/attributes.phtml (or copy this file in your theme from base or default custom theme):

<?php foreach ($_additional as $_data):
// Add these 2 lines
$_test_data_value = trim($_data['value']);
if ((empty($_test_data_value) || in_array($_test_data_value, array(Mage::helper('catalog')->__('N/A'), Mage::helper('catalog')->__('No'))))) continue;?>

Below is not necessary to achieve what you've asked:

Those attributes are still loaded. To optimize this (if you have a big number of attributes in attributes sets) do:

public function getAdditionalData(array $excludeAttr = array())
{
    $data = array();
    $product = $this->getProduct();
    $attributes = $product->getAttributes();
    foreach ($attributes as $attribute) {
//            if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
        if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {

            // Fix:
            //$value = $attribute->getFrontend()->getValue($product);

            if (!$product->hasData($attribute->getAttributeCode())) {
                $value = Mage::helper('catalog')->__('N/A');
            } 
            // Fix:
            elseif ((string) ($value = $attribute->getFrontend()->getValue($product)) == '') {
                $value = Mage::helper('catalog')->__('No');
            } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                $value = Mage::app()->getStore()->convertPrice($value, true);
            }

            if (is_string($value) && strlen($value)) {
                $data[$attribute->getAttributeCode()] = array(
                    'label' => $attribute->getStoreLabel(),
                    'value' => $value,
                    'code'  => $attribute->getAttributeCode()
                );
            }
        }
    }
    return $data;
}

Note the two // Fix: comments.

This modified function is from Mage_Catalog_Block_Product_View_Attributes. You need to copy the function above in your block class from your module. Your block class rewrites the core block class. Applying this will improve considerably product view page load on frontend.

If you don't know how to create a custom module in local dir than search a tutorial on how to create a Magento module and how to rewrite a core block class. Or try http://www.magentocommerce.com/magento-connect/ultimate-module-creator.html.

Related Topic