1

i did some coding to group the product attributes on frontend and show their groups names above theme like this:

  • attgroup 1
    • attribute 1
    • attribute 2
    • ...
  • attgroup 2
    • attribute 3
    • attribute 4
    • ...

this is my groupview.phtml (atributes.phtml alternative) content:

<?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++; ?>
    <div class="attributesgroups-title">
        <h3 style="margin:0; color:white;"><?php echo $this->__( $_additional['title'] )?></h3>
        </div>
        <table style="border-radius: 0 0 2px 2px;" 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"><img src="<?php echo $this->getSkinUrl('images/attributes/'.$_data['code'].'.png')?>" alt="<?php echo $this->htmlEscape($this->__($_data['label'])) ?>" /><span class="separator">|</span><?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;?>

and this is my groupview.php:

<?php 

class Webguys_AttributesAsGroup_Block_Groupview extends Mage_Core_Block_Template
{
    protected $_product = null;

    function getProduct()
    {
        if (!$this->_product) {
            $this->_product = Mage::registry('product');
        }
        return $this->_product;
    }

    public function getAdditionalData(array $excludeAttr = array())
    {
        $data = array();
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {
//            if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {

                $value = $attribute->getFrontend()->getValue($product);

                // TODO this is temporary skipping eco taxes
                if (is_string($value)) {
                    if (strlen($value) && $product->hasData($attribute->getAttributeCode())) {
                        if ($attribute->getFrontendInput() == 'price') {
                            $value = Mage::app()->getStore()->convertPrice($value,true);
                        }

                        $group = 0;
                        if( $tmp = $attribute->getData('attribute_group_id') ) {
                            $group = $tmp;
                        }

                        $data[$group]['items'][ $attribute->getAttributeCode()] = array(
                           'label' => $attribute->getFrontend()->getLabel(),
                           'value' => $value,
                           'code'  => $attribute->getAttributeCode()
                        );

                        $data[$group]['attrid'] = $attribute->getId();

                    }
                }
            }
        }

        // Noch Titel lesen
        foreach( $data AS $groupId => &$group ) {
            $groupModel = Mage::getModel('eav/entity_attribute_group')->load( $groupId );
            $group['title'] = $groupModel->getAttributeGroupName();
        }

        return $data;
    }    


}

i customized my code to hide attributes with empty values from this tab. its working fine but their group titles are still showing. i want to hide group titles of attribute groups which doesn't contain any filled attributes.

hide att group

9
  • Can you include in your post some screenshots with empty attribute groups to understand you more clearly
    – zhartaunik
    May 8, 2015 at 13:47
  • @zhartaunik I added image. can you check it out please?
    – Moh3n
    May 8, 2015 at 16:06
  • @Moh3n Did you found a working solution? I'm having the same problem.
    – JGeer
    May 19, 2015 at 8:27
  • @Jelle Yes its working
    – Moh3n
    May 19, 2015 at 11:54
  • @Moh3n Can you provide the working code? Because I'm looking for the same solution as you asked for, but give solution is not working. I'm getting things like ' [47] => array(4) { ["items"] => array(2) { ["licensed_by"] => array(3) { ["label"] => string(19)...'
    – JGeer
    May 19, 2015 at 11:59

1 Answer 1

2

I suggest that what you should do is when your generating the array in getAdditionalData, you pre define if the group has actually got any values worth displaying the group. Then in the template you can do a simple check before actually rendering any of the block.

    public function getAdditionalData(array $excludeAttr = array())
    {
        $data = array();
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        //Zend_Debug::dump($attributes);
        foreach ($attributes as $attribute) {
//            if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            // Zend_Debug::dump($attribute->getIsVisibleOnFront() );
            if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {

                $value = $attribute->getFrontend()->getValue($product);


                // TODO this is temporary skipping eco taxes
                if (is_string($value)) {
                    if (strlen($value) && $product->hasData($attribute->getAttributeCode())) {
                        if ($attribute->getFrontendInput() == 'price') {
                            $value = Mage::app()->getStore()->convertPrice($value,true);
                        }

                        $group = 0;
                        if( $tmp = $attribute->getData('attribute_group_id') ) {
                            $group = $tmp;
                        }

                        $data[$group]['items'][ $attribute->getAttributeCode()] = array(
                            'label' => $attribute->getFrontend()->getLabel(),
                            'value' => $value,
                            'code'  => $attribute->getAttributeCode()
                        );

                        // If there has been a value set above, we know this group is worth showing.
                        if(strlen($data[$group]['items'][$attribute->getAttributeCode()]['value']) && $data[$group]['items'][$attribute->getAttributeCode()]['value'] !== "No") {

                            /* Optionally, 
                             * If you require No as a valid option, verify those attributes here. 
                             * @example 
                             * if($attribute->getAttributeCode() == "yesNoOption") {
                             * $data[$group]['values_exist'] = true;
                             * } else {
                             * $data[$group]['values_exist'] = false;
                             * } 
                             */
                            // Set the group to record that it has values
                            $data[$group]['values_exist'] = true;
                        }
                        $data[$group]['attrid'] = $attribute->getId();

                    }
                }
            }
        }

        // Noch Titel lesen
        foreach( $data AS $groupId => &$group ) {
            $groupModel = Mage::getModel('eav/entity_attribute_group')->load( $groupId );
            $group['title'] = $groupModel->getAttributeGroupName();

        }

        return $data;
    }

With the above modification, you will then be able to in your template do a quick check on it and skip to the next record before doing any of the html output.

    <?php Zend_Debug::dump($_additionalgroup); ?>
    <?php $i=0; foreach ($_additionalgroup as $_additional): $i++;
        Zend_Debug::dump($_additional);
        if(!isset($_additional['values_exist']) || $_additional['values_exist'] !== true) {
            continue;
        }

    ?>

Hope that helps.

Update

I setup an example environment locally to debug why you might be experiencing continued problem. I noticed on one of them I was getting a default value of "No" back for the attribute. I have updated the code above to handle this - if there is no value, or the value returned is "No" then it will make it so that they are considered not valid.

If however you have attributes which do actually have the value of "No" as a legitimate display option you'll need to modify the code (look at the comment I've made in the Block for an example).

10
  • can you please check my answer. i have putted my edited files for you. i changed my files with your method but now all my group titles are hidden. i just want empty ones to get hide.
    – Moh3n
    May 9, 2015 at 4:28
  • @Moh3n I have updated my answer with debug statements where I was suggesting. I think whats happening is your getting a default value back, which means the check for string length is not correctly identifing it - ie "No" is a valid response. May 9, 2015 at 15:13
  • its not working. its getting something like this in additional tab: ' [47] => array(4) { ["items"] => array(2) { ["licensed_by"] => array(3) { ["label"] => string(19)...'
    – Moh3n
    May 10, 2015 at 5:38
  • @Moh3n it means your getting a valid response then for ones that you believe should be invalid. Please post the full result so we can see what other conditions are needing to be detected. And highlight the ones which you believe are incorrect. May 10, 2015 at 7:13
  • would you pleas check my debug report please?
    – Moh3n
    May 11, 2015 at 6:53

Your Answer

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

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