Magento – How to add check box in system configuration

adminconfigurationmagento-1.9system.xml

How do you add a check box in the system configuration & how do you save that check box value in Database?

<terms translate="label">
    <class>terms-conditions</class>
    <frontend_type>checkboxes</frontend_type>
    <validate>required-entry</validate>
    <frontend_model>mymodule/system_config_form_terms</frontend_model>
    <sort_order>15</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>0</show_in_website>
    <show_in_store>0</show_in_store>
</terms>

Terms.php

<?php
/**
 * Adminhtml customer grid block
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class NameSpace_Mymodule_Block_System_Config_Form_Terms extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $html = "
       <input type='checkbox' class='required-entry' name='terms' value='1'> Terms and Conditions (<a href='#'>Click Here</a>)";
        return $html;
    }
}
?>

Best Answer

The following doesn't work. It seems we can't set a value via system.xml for the checkbox.

Magento has two different checkbox types:

  • checkbox
  • checkboxes

I think you want the first, therefore you have to change the type:

<terms translate="label">
    <class>terms-conditions</class>
    <frontend_type>checkbox</frontend_type>
    <validate>required-entry</validate>
    <frontend_model>mymodule/system_config_form_terms</frontend_model>
    <sort_order>15</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>0</show_in_website>
    <show_in_store>0</show_in_store>
</terms>