We use this way to display attribute group names on the additional information tab on product page. Currently when a attribute got no data, the entire attribute will be hidden. I want to do the same for the attribute group name. So that if the attribute group got no attributes with data, it needs to be hidden.
This is our code:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<div class="box-collateral box-additional">
<h2><?php echo $this->__('Additional Information') ?></h2>
<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
<h3><?php echo $this->__( $_additional['title'] )?></h3>
<table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional['items'] 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-<?php echo $i?>')</script>
<?php endforeach; ?>
</div>
<?php endif;?>
How can I fix this?
-
Create if() statement before <h3> tag and check if your requirements are satisfied. That's not Magento issue.Amberta– Amberta2015年01月28日 10:42:30 +00:00Commented Jan 28, 2015 at 10:42
-
I know, but I can't get it done. Do you know what if() statement I need for this?JGeer– JGeer2015年01月28日 11:08:24 +00:00Commented Jan 28, 2015 at 11:08
2 Answers 2
How out looping through the additional data once, collecting all the valid attributes in an array and then looping through that array again and skip over the empty groups.
Something like this (untested code):
$values = array();
foreach ($_additionalgroup as $_additional) {
if (!isset($values[$_additional['title']])) {
//maybe $_additional['title'] is not the best key here. See if you have an id or something unique available.
$key = $_additional['title'];
$values[$key] = array();
$values[$key]['title'] = $_additional['title'];
$values[$key]['attributes'] = array();
foreach ($_additional['items'] as $_data) {
$_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) {
$values[$key]['attributes'][] = array(
'label' => $_data['label'],
'value' => $_helper->productAttribute($_product, $_data['value'], $_data['code'])
);
}
}
}
}
now loop through $values and print only sections where count($values[$key]['attributes']) is not zero.
<?php $i=0;foreach ($values as $key => $data) : ?>
<?php if (count($data['attributes']) > 0) : ?>
<h3><?php echo $this->__( $data['title'] )?></h3>
<table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($data['attributes'] as $attribute) : ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($attribute['label'])) ?></th>
<td class="data"><?php echo $attribute['value'] ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endif;?>
<?php endforeach;?>
-
I tried the code, but it does not work. The entire additional information tab is empty.JGeer– JGeer2015年02月11日 07:30:42 +00:00Commented Feb 11, 2015 at 7:30
-
It reads "(untested code)". It's just a suggestion for you to look into it and improve it.Julien Lachal– Julien Lachal2016年08月30日 08:15:54 +00:00Commented Aug 30, 2016 at 8:15
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; ?>
<?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; ?>
Explore related questions
See similar questions with these tags.