I want to add a filed in the Magento 2 admin panel, this field must return in the frontend True if in admin is selected Yes. I try to add this in the system.xml:

<field id="do_something" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Do something?</label>
    <comment>Do we want to do something?</comment>

but this is return only 1 and 0 value and I need to have true or false;

What I can do?

Thank you


<?php if ($config->getDoSomething()): ?>'<?php echo $config->getDoSomething(); ?>',<?php else: ?><?php endif; ?>
  • you can treat 1 as true and 0 false, or if you want true/false directly you have to create your custom source model – Piyush Nov 29 '17 at 13:18
  • thanks how I can treat 1 as true? – Robert Nov 29 '17 at 13:20
  • how are you using this value in frontend ? please add code – Piyush Nov 29 '17 at 13:22
  • please take a look I update my post – Robert Nov 29 '17 at 13:26

Try below code

<?php $doSomething = ($config->getDoSomething() ? 'true' : 'false') ?>
<?php echo $doSomething; ?>'
| improve this answer | |

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.