0

Could someone help me i want to hide the attribute if its have empty vale on product details page.

Example

<?php // START FABRIC ATTRIBUTE
    $attributeName = $_product->getResource()->getAttribute('fabric_composition');
    if ($attributeName) {
    $labelValue = $attributeName->getFrontend()->getValue($_product);
        }
    echo $labelValue;
    // END FABRIC ATTRIBUTE
      ?>

I have fabric_composition attribute i want to add if condition and want to hide if attribute value is empty Thankyou

1 Answer 1

0

You may use this Syntax:

<?php // START COLOUR ATTRIBUTE ?>
<?php $attributeName = $_product->getResource()->getAttribute('fabric_description');?>
<?php if ($attributeName):?>
<?php $labelValue = $attributeName->getFrontend()->getValue($_product);?>
<?php echo $labelValue;?>
<?php else:?>
<?php $labelValue ='';?>
<?php  echo $labelValue; ?>
<?php endif;?>
12
  • Sorry getting syntax error <?php // START COLOUR ATTRIBUTE $attributeName = $_product->getResource()->getAttribute('fabric_description'); if ($attributeName) { $labelValue = $attributeName->getFrontend()->getValue($_product); } echo $labelValue; else: l$abelValue =''; echo $labelValue; ?>
    – user51556
    Oct 27, 2021 at 11:23
  • OH! you have just copied and pasted code at least look while copying code< See updated answer use that in phtml Oct 27, 2021 at 11:29
  • NOTE: You can also remove else if you wish use only if condition Oct 27, 2021 at 11:31
  • Thanks very much its working fine! One help more i also want to show attribute label with attribute value. I much appreciate your help
    – user51556
    Oct 27, 2021 at 11:36
  • Good; please vote up to my answer as helpful thankyou and welcome yes you can echo label with value easily Oct 27, 2021 at 11:44

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.