1

We have a lot of different products with different attributes we want to show below the product in the grid view.

What is the best approach in order to show the specs below when you have a lot of different attributes and every category has different attributes to show.

<?php 

    $attr_levensduur            = $_product->getAttributeText('levensduur');
    $attr_formaat_bodembedekker = $_product->getAttributeText('formaat_bodembedekker');
    $attr_kubischemeter         = $_product->getAttributeText('kubischemeter');
    $attr_aswaarde              = $_product->getAttributeText('aswaarde');
    $attr_warmtewaarde          = $_product->getAttributeText('warmtewaarde');
    $attr_houtsoort             = $_product->getAttributeText('houtsoort');

?>

<?php if ($attr_levensduur || $attr_formaat_bodembedekker || $attr_kubischemeter || $attr_aswaarde || $attr_warmtewaarde || $attr_houtsoort) : ?>
<ul class="specs-list list-unstyled">
    <?php if (!empty ($attr_levensduur)) : ?>
        <li class="small"><?php echo $attr_levensduur; ?></li>
    <?php endif; ?>
    <?php if (!empty ($attr_formaat_bodembedekker)) : ?>
        <li class="small"><?php echo $attr_formaat_bodembedekker; ?></li>
    <?php endif; ?>
    <?php if (!empty ($attr_kubischemeter)) : ?>
        <li class="small"><?php echo $attr_kubischemeter; ?></li>
    <?php endif; ?>
    <?php if (!empty ($attr_aswaarde)) : ?>
        <li class="small"><?php echo $attr_aswaarde; ?></li>
    <?php endif; ?>
    <?php if (!empty ($attr_warmtewaarde)) : ?>
        <li class="small"><?php echo $attr_warmtewaarde; ?></li>
    <?php endif; ?>
    <?php if (!empty ($attr_houtsoort)) : ?>
        <li class="small"><?php echo $attr_houtsoort; ?></li>
    <?php endif; ?>
</ul>
<?php endif; ?>

When we have for example 50 different attributes this approach is not the best one i guess.

See the image below for an example

Example attributes on grid view

1

Since Magento already look up all associated product data (if enabled under the specific attribute) by category you could avoid the first query altogether.

<?php 

    $attr_levensduur            = $_product->getAttributeText('levensduur');
    $attr_formaat_bodembedekker = $_product->getAttributeText('formaat_bodembedekker');
    $attr_kubischemeter         = $_product->getAttributeText('kubischemeter');
    $attr_aswaarde              = $_product->getAttributeText('aswaarde');
    $attr_warmtewaarde          = $_product->getAttributeText('warmtewaarde');
    $attr_houtsoort             = $_product->getAttributeText('houtsoort');

?>

Then populate grid and list view by attribute set ID or attribute name.

<?php if( $_product->getAttributeSetId()('ID') ): ?>
<?php echo $_product->getData('ATTRIBUTE NAME') ?>
<?php endif;?>

<?php if( $_product->getData()('ATTRIBUTE NAME') ): ?>
<?php echo $_product->getData('ATTRIBUTE NAME') ?>
<?php endif;?>

That could clean it up a tiny bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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