Saturday, March 26, 2016

Hide Magento Attributes with no Value


Hide Magento Attributes with no Value

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:

Size:N/A
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:

   
        htmlEscape($this->__($_data['label'])) ?>
        productAttribute($_product, $_data['value'], $_data['code']) ?>
   

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

    getResource()->getAttribute($_data['code']);
    if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
   
        htmlEscape($this->__($_data['label'])) ?>
        productAttribute($_product, $_data['value'], $_data['code']) ?>
   
   


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

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

0 comments:

Post a Comment