12

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.

1
  • We will need quite a lot more information in order to help (which attribute? where?)
    – benmarks
    Jul 19, 2014 at 19:37

9 Answers 9

7

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.

1
  • First solution changing the template file is fine, but there are two issues. First if there is an attribute type Yes/No with a value set to No, it will be hidden in frontend which is not OK. Second if there are no attributes you will get the header Additional Information which is not OK. If there are no attributes that header should not appear.
    – ADDISON74
    Jan 3, 2017 at 22:17
6

Find and open the attributes.phtml file. This file can be found here: /app/design/frontend/[YOUR PACKAGE]/[YOUR THEME]/template/catalog/product/view/attribute.phtml

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

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

Source: http://www.magthemes.com/magento-blog/empty-attributes-showing-na-fix/

4

I don't know exact, but I have read it somewhere.

Hide the empty attributes by just edit the template file named as “attributes.phtml”.

In your code, find 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; ?>

and replace these lines with these:

<?php foreach ($_additional as $_data): ?>
    <?php if ((string)$_data['value'] != '' and $_data['value'] != 'N/A'): ?>
        <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 endif; ?>
<?php endforeach; ?>
1
  • 1
    Your solution only hides attribute type datetime which is the only one having N/A value. A text field, text area, multiselect, dropdown has No value. If the attribyte type is datetime and value is set to No it should be shown instead of hidding.
    – ADDISON74
    Jan 3, 2017 at 22:21
1

change the following code in app/design/frontend/base/default/template/catalog/product/view/attributes.phtml:

from:

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

to:

<?php foreach ($_additional as $_data): ?>
<?php if ($_product->getAttributeText($_data['code']) == '') continue; ?>
<tr>
    <th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
    <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
1
  • 2
    Dont change the base templates... May 21, 2016 at 15:32
1

In your custom theme, navigate to: catalog\product\view\attributes.phtml. Your PHP code should check if the attribute value is "No" or "N/A" in all languages. This will not render the attributes with these values.

The code will look like this:

<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
$emptyValues = array($this->__('N/A'), $this->__('No'));
?>
<?php if($_additional = $this->getAdditionalData()): ?>
    <h2><?php echo $this->__('Additional Information') ?></h2>
    <table class="data-table" id="product-attribute-specs-table">
        <col width="25%" />
        <col />
        <tbody>
        <?php foreach ($_additional as $_data): ?>
            <?php if(!in_array($_data['value'], $emptyValues)): ?>
                <tr>
                    <th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
                    <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                </tr>
            <?php endif; ?>
        <?php endforeach; ?>
        </tbody>
    </table>
    <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>

The variable $emptyValues is added and the check if it's in the array has been added to the code.

Make sure to empty cache after you have done changes to the frontend.

1
  • not working above code for me
    – Gem
    Apr 13, 2017 at 6:20
1

This can be done with a small piece of code. Find and open the attributes.phtml file. This file can be found here: /app/design/frontend/[theme name]/[package name]/template/catalog/product/view/attribute.phtml

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

Problem solved :) Solution is here : http://www.magentocommerce.com/boards%20/viewthread/294064/#t407742

This module works fine with magento 1.8.1 No need to purchase module or edit any code.

Thanks Niro (Developer of this module)

2
  • 1
    The link is broken Can I know update live link?
    – Yohan
    May 21, 2015 at 8:44
  • 4
    or even better: post the correct answer here. A new link will become broken again... Jul 13, 2015 at 14:03
0

Easy way, but not necessary better than others.

Update your translate file Mage_Catalog.csv. Set empty values like below.

N/A,""
No,""

Frontend attributes will be ignored when No or N/A.

0

Sometimes we come across a store that wants to have many different product attributes, but they only want the default attribute set. This means that every product will have lets say 10+ options that sometimes don’t apply to certain products. For instance a piece of clothing might need a size attribute, but a piece of furniture doesn’t. Because the store uses the same attribute sets for each products the empty size attribute will show up like this:

This is ofcourse very confusing for customers, so the better option would be to hide attribute value’s that are empty. This can be done with a small piece of code. Find and open the attributes.phtml file. This file can be found here:app/design/frontend/default/[theme name]/template/catalog/product/view/attribute.phtml

Open the file and search for the following lines:

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

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

Thats it! Empty attributes will now be hidden from your product pages. Don’t forget to refresh your cache to see the changes.

SOurce: https://tejabhagavan.blogspot.in/2016/03/hide-magento-attributes-with-no-value-2.html

1
  • not working above code
    – Gem
    Apr 13, 2017 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.