Display text if attribute yes/no is set to YES in Magento2?
-
11-12-2020 - |
Question
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
Solution
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange