0

I use this code to display one attribute in frontend:

<?php 
    $optionText = '';
    $attr = $_product->getResource()->getAttribute('newbrand_name');

    if ($attr->usesSource()) {
       $optionText = $attr->getSource()->getOptionText($_product->getNewbrandName());
    }
?>
<?php if($optionText){ ?>
    <div class="attributes-list"><?php echo "Producent: " ?><?php echo $optionText ?></div>
<?php } ?>

Working correctly.

Can anyone help me modify this code to display two additional attributes? Color and Gender from attribute code:

  • color

  • gender

1 Answer 1

1

You can use getAttributeText to get the value of product attribute.

<?php 
    $optionText = '';
    $attr = $_product->getResource()->getAttribute('newbrand_name');

    if ($attr->usesSource()) {
       $optionText = $attr->getSource()->getOptionText($_product->getNewbrandName());
    }
?>
<?php if($optionText){ ?>
    <div class="attributes-list"><?php echo "Producent: " ?><?php echo $optionText ?></div>
<?php } ?>

<?php if($_product->getAttributeText('color')) : ?>
    <?php $optionValue = $_product->getAttributeText('color'); ?>
    <div class="attributes-list"><?php echo "Color: " ?><?= $optionValue; ?></div>
<?php endif; ?>

<?php if($_product->getAttributeText('gender')) : ?>
    <?php $optionValue = $_product->getAttributeText('gender'); ?>
    <div class="attributes-list"><?php echo "Gender: " ?><?= $optionValue; ?></div>
<?php endif; ?>
2
  • working perfect. I have the last question. Do you have any solution how to display this all attributes in one line? prnt.sc/ukw7pq
    – Sylvester
    Commented Sep 21, 2020 at 6:32
  • sorry, that's the CSS change you need to do for that. Commented Sep 21, 2020 at 8:03

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.