Magento Forum

Page 1 of 2
Can’t hide custom attribute titles, when value is null
 
jrutter
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

So I have added a bunch of custom attributes, and I want the title only to show up when there is a value in the custom attribute field, so for example:

When value is present, this is shown:

<h4>Ingredients</h4>

No Value:

<h4></h4>

Here is the code that I have put together, but its not working - anyone have any ideas?

<p><?php echo $_product->getResource()->getAttribute('product_benefits')->getFrontend()->getValue($_product
        if (
$_product == 'null')
            echo 
'<h4></h4>';
            else
            echo 
'<h4>Product Benefits</h4>';
?></p>
 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

You’re testing if the $_product is null there, not the attribute. Further more, you are echo-ing out that value first before attempting to output the “Product Benefits” header. (’echo’ in php prints anything to the HTML page so it becomes visible.)

Does ‘$_product->getProductBenefits()’ work or do you need to use ‘echo $_product->getResource()->getAttribute(’product_benefits’)->getFrontend()->getValue($_product)’ ??

if($_product->getProductBenefits() != 'NULL' || $_product->getProductBenefits() != ''//test if the attribute value is null or empty
    
echo '<h4>Product Benefits</h4>';
    echo 
'<p>'$_product->getProductBenefits() .'</p>';  //echo the attribute value 
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
jrutter
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

Thanks for replying, Im sort of confused by your reply. Im not really sure, all I know is that the first line outputs the custom attribute, but I would like it to check to see if there is a value - and if there is nothing, then both the title and value dont show…

 
Magento Community Magento Community
Magento Community
Magento Community
 
jrutter
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

I figured it out, by searching the forums and finding the ideal code.

here is it:

<?php if ($_product->getResource()->getAttribute('how_to_use')->getFrontend()->getValue($_product)):?>
                                    
<h4><?php echo $_product->getResource()->getAttribute('how_to_use')->getFrontend()->getLabel($_product?></h4>
                                         
<?php echo $_product->getResource()->getAttribute('how_to_use')->getFrontend()->getValue($_product?>
                                <?php 
endif;?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
lurebeauty
Sr. Member
 
Avatar
Total Posts:  229
Joined:  2009-07-06
 

jrutter

Where did you place this code?  I’m having a hard time, where the attributes keep showing up inside the tabs even when blank.  Screenshot here:  Im not sure where to place the code, i.e which file?

Image Attachments
Clip.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
 
Novusweb
Sr. Member
 
Avatar
Total Posts:  89
Joined:  2009-05-06
Texas
 

Here’s what I altered in attributes.phtml in order to hide product attributes that have no value:

<tbody>
        
<?php foreach ($_additional as $_data): ?>
            <?php 
if(($_data['value']) != 'No'): ?>
            
<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; ?>
        
</tbody>

I just added the IF statement to test for a value in the attribute. Seemed simple enough.

Hope this helps others.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MyProvider
Jr. Member
 
Total Posts:  4
Joined:  2009-02-08
 

If you have multiple languages (or values to exclude) you can do this;

(add <?php if ($_data['value'] != 'No' && $_data['value'] != 'Nee' ): ?> plus the endif statement, watch the : )

<<<PART OF attributes.phtml in the template app dir>>>

<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<div class="add_info_table">
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<?php if ($_data['value'] != 'No' && $_data['value'] != 'Nee' ): ?>
<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; ?>
</tbody>
</table>
</div>
<script type="text/javascript\">decorateTable(’product-attribute-specs-table’)</script>
<?php endif;?>

<<<SCRIPT END>>>

The correct way would be to change the GetAdditionalData funtion to not return any value when value is null or return a specific value like EMPTY, then the attributes.phtml can filter out empty results, since in some cases you might want to set a value to No for a product....

 
Magento Community Magento Community
Magento Community
Magento Community
 
hammock_vienna
Member
 
Total Posts:  60
Joined:  2008-06-16
 

sorry to say - but that’s not “beautiful” grin
searching for nee, no, nein, etc.. makes it hard when you enhance to more languages later. I guess the right way would be to retrieve the value of the admin store of “no” etc. But no idea how to get that one… Any hint?

 
Magento Community Magento Community
Magento Community
Magento Community
 
MyProvider
Jr. Member
 
Total Posts:  4
Joined:  2009-02-08
 

It is a very bad solution indeed, but it does work, as a short term work around for my production sites,
until someone makes the effort to make a clean solution and post it wink....

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tusher Khan
Member
 
Total Posts:  66
Joined:  2011-01-07
 

You can easily hide custom attribute titles, when your value is null.

Here is a simple way to do that:

http://www.xpressbangla.com/hide-attributes-from-frontend-which-values-are-not-given-from-backend-in-magento/

 
Magento Community Magento Community
Magento Community
Magento Community
 
kanted
Jr. Member
 
Total Posts:  12
Joined:  2008-09-20
 

Did work perfectly using Tusher Khan’s link said in above post on 1.7.0.0 ver

 
Magento Community Magento Community
Magento Community
Magento Community
 
salimsazzad
Jr. Member
 
Total Posts:  19
Joined:  2011-07-02
 

Hi, you can hide custom attribute titles, when value is null just following the below link :

http://www.salimsazzad.com/hiding-attributes-values-from-magento-frontend-which-are-not-given-in-backend

 
Magento Community Magento Community
Magento Community
Magento Community
 
cyber5
Jr. Member
 
Total Posts:  5
Joined:  2012-12-28
 
Novusweb - 07 January 2011 11:25 AM

Here’s what I altered in attributes.phtml in order to hide product attributes that have no value:

I just added the IF statement to test for a value in the attribute. Seemed simple enough.

Hope this helps others.

It helped me, Novus! I’m now using Magento 1.7, with custom tabs on the Classic Theme from templates-master. Your code was simple and worked great. Thanks.  grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
Cicchellia
Jr. Member
 
Total Posts:  7
Joined:  2012-11-11
 

Worked well...Thanks! Look forward to working with Magento. Wonderful software.

<a href="www.pallet-of.com"Pallet Of"</a>

Best selling pallets for all you Ebay and Amazon needs.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MageClub
Guru
 
Avatar
Total Posts:  386
Joined:  2009-07-25
India
 

For latest Magento 1.7.x , here is the tested code,

http://www.learnmagento.org/magento-tips-tricks/hide-magento-custom-attribute-when-value-is-null/

 
Magento Community Magento Community
Magento Community
Magento Community
 
harryleit
Jr. Member
 
Total Posts:  2
Joined:  2010-12-27
 
Novusweb - 07 January 2011 11:25 AM

Here’s what I altered in attributes.phtml in order to hide product attributes that have no value:


<tbody>
        
<?php foreach ($_additional as $_data): ?>
            <?php 
if(($_data['value']) != 'No'): ?>
            
<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; ?>
        
</tbody>

I just added the IF statement to test for a value in the attribute. Seemed simple enough.

Hope this helps others.

THANK YOU Novusweb. Currently using Magento 1.7 and this worked great.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2