1

I try to display two custom attributes in the product view page but this attributes is not exist for all products. How I can check if current product contain one of this two attributes?

I use this code to get the attributes value:

<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('data_sheet')->getFrontend()->getValue($_product); ?>
<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('health_sheet')->getFrontend()->getValue($_product); ?>

1 Answer 1

2

Try this

<?php 
    $dataSheet = $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->getDataSheet(), 'data_sheet'); 
    $healthSheet = $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->getHealthSheet(), 'health_sheet'); 
    if(isset($dataSheet) || isset($healthSheet)){
        //display your DIV
    }
?>
6
  • but I have two attributes, I need to check if one of this two attributes have values. For example first can have and the second not, in this case I still need to display the entire div ...
    – Robert
    Commented Oct 31, 2017 at 17:18
  • check my updated answer Commented Oct 31, 2017 at 17:20
  • is not work, I don't see any values when I try to echo that attrbutes
    – Robert
    Commented Oct 31, 2017 at 17:27
  • In which file you are try to print the attribute? Commented Oct 31, 2017 at 17:38
  • in this Magento_Catalog/templates/product/view/form.phtml
    – Robert
    Commented Oct 31, 2017 at 17:43

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.