2

I'm trying to display something in the product page if a YES/NO attribute is set to YES. Actually the only way I found to do this in multilingual magento 2.1.8 is :

if($_product->getAttributeText('MY_ATTRIBUTE') == 'Oui' || $_product->getAttributeText('MY_ATTRIBUTE') == 'Yes') {
    echo "Set to Yes";
} else {
    echo "Set to No";
};

This method needs to test the YES/NO answer for every language but I'm pretty sure there's a better way to do this...

Thanks for your help

3

You should use boolean value instead of string to check Yes/No value, use

$_product->getData('attribute_code');

<?php 

$yesNo = $_product->getData('attribute_code');

if($yesNo) {
    //value is set to Yes
} else {
    //value is set to No
}

?>

For example, if your attribute code is myattribute then you can also use get() method to get attribute method

$_product->getMyattribute();

  • done but not enough reputation for now, new on this forum... but still thanks a lot – urbansurfers Sep 4 '17 at 4:01

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.