1

I have an HTML table which displays the attributes of a product inside a block.

<table id="specification-table" style="width:100%">
 <tbody>
  <tr>
   <th>Light Color: </td>
   <td><p>{{block type="core/template"  template="catalog/product/view/light_color.phtml"}}<p></td>
  </tr>
  <tr>
   <th>Light Source: </td>
   <td><p>{{block type="core/template"  template="catalog/product/view/light_source.phtml"}}<p></td>
  </tr>
 </tbody>
</table>

<style>
table#specification-table
{
border: 1px solid black;
border-collapse: collapse;
}
</style>

The PHTML file looks like this: (for light_source it is pretty much the same except 'light_color' is replaced with 'light_source' )

<?php
$product_id = Mage::registry('current_product')->getId();
$_product= Mage::getModel('catalog/product')->load($product_id);
$attribute = $_product->getResource()->getAttribute('light_color');
?>
<?php if ($attribute): ?>
<div class="std">
<?php echo $attribute_value = $attribute ->getFrontend()->getValue($_product);  ?>
</div>
<?php endif; ?>

It works but now it does this for every single product even if it doesn't contain the attribute, if it doesn't contain the attribute it just displays an empty row. Now comes my problem: I would like to have a way to check if the product contains the selected attribute. If it does it just displays it, if it doesn't contain it, it just doesn't add it to the table. Any help would be much appreciated!

2 Answers 2

1

Try

if ($product->getData('light_color')) {
    //your code here
}

or

if ($product->hasData('light_color')) {
    //your code here
}
2
  • In the PHTML that would be the solution, but i think my problem lays within my html table because I still add it in my table, is there a way i can just let the table fill itself with the information of the attribute if the products contains the attribute and if it doesn't it just doesn't fill in that information?
    – jscholten
    Nov 1, 2016 at 13:27
  • Unfortunately this only works if the data is set. If you want to find out if the specific attribute is in the products attribute set...you need a different method.
    – tread
    Jan 20, 2017 at 9:48
1

Before rendering the table, you have to check whether the attributes exists or not. So your phtml code should be like

<?php
$product_id = Mage::registry('current_product')->getId();
$_product= Mage::getModel('catalog/product')->load($product_id);
$attribute_color = $_product->getResource()->getAttribute('light_color');
$attribute_source = $_product->getResource()->getAttribute('light_source');
?>

<?php if ($attribute_color && $attribute_source){ ?>
<table id="specification-table" style="width:100%">
<tbody>
<?php if ($attribute_color){ ?>
 <tr>
  <th>Light Color: </td>
  <td><p>{{block type="core/template"  template="catalog/product/view/light_color.phtml"}}<p></td>
 </tr>
<?php } ?>
<?php if ($attribute_source){ ?>
 <tr>
  <th>Light Source: </td>
  <td><p>{{block type="core/template"  template="catalog/product/view/light_source.phtml"}}<p></td>
 </tr>
<?php } ?>
</tbody>
</table>
<?php } ?>

Your Answer

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

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