1

This question already has an answer here:

I need to hide the attributes table if the attribute is empty. This is my code in the attributes.phtml file

<?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 $_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; ?>
        </tbody>
    </table>
    <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>

So the attribute isn't showing, but the attributes-spec-table still is. How do I hide this table if there is only an empty attribute?

marked as duplicate by sv3n, Piyush, Keyur Shah, Jai, Manoj Deswal Jan 9 '18 at 0:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Update - since I felt a bit bad for this dirty answer - maybe that one is easier on the eye for others as well:

<?php if($_additional = $this->getAdditionalData()): ?>  
  <?php $attrTableBody = false; // empty table body ?>
  <?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']); ?>
    <?php if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')): ?>
      <?php // put the table body parts into $attrTableBody ?>
      <?php $attrTableBody .= "<tr><th class='label'>".$this->htmlEscape($this->__($_data['label']))."</th><td class='data'>".$_helper->productAttribute($_product, $_data['value'], $_data['code'])."</td></tr>" ?>
    <?php endif; ?>
  <?php endforeach; ?>
  <?php if($attrTableBody != false): // if table body got set ?>
    <h2><?php echo $this->__('Additional Information') ?></h2>
    <table class="data-table" id="product-attribute-specs-table">
      <col width="25%" />
      <col />
      <tbody>
        <?php echo $attrTableBody; // print the table body ?>
      </tbody>
    </table>
    <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
  <?php endif; ?>
<?php endif;?>

Accepted answer:

I don't claim to have the most pretty approach to this, but you could do something like this:

<?php if($_additional = $this->getAdditionalData()): ?>

  <?php $attrFirst = true; // will be set to false after first attribute is to be displayed ?>
  <?php $attrCnt = 0; // current attribute number ?>
  <?php $attrCntTotal = count($_additional); // number of all attributes ?>

  <?php foreach ($_additional as $_data): ?>

      <?php $attrCnt++; ?>

      <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
      if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>

      <?php if($attrFirst): // create table structure if its the first attribute to be displayed ?>
        <?php $attrFirst = false; ?>
        <h2><?php echo $this->__('Additional Information') ?></h2>
        <table class="data-table" id="product-attribute-specs-table">
        <col width="25%" /><col />
        <tbody>
      <?php endif; ?>

      <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 if($attrCntTotal == $attrCnt && $attrFirst == false): // close table struvture if its the last attribute to be displayed (and it was opened previously) ?>
        </tbody>
        </table>
        <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
      <?php endif; ?>

      <?php } ?>
  <?php endforeach; ?>
<?php endif;?>

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