2

iv created custom checkboxes in the checkout page through the functions.php file but i would like one of them to be ticked how do i do this ?

woocommerce_form_field( 'my_checkbox1', array( 
'type' => 'checkbox', 
'class' => array('input-checkbox'), 
'label' => __('Standard Shipping (2–7 Days, FREE!) <span>Most items are shipped FREE OF CHARGE within Thailand.</span>'), 
'required' => false, 
'value'  => true, 
), $checkout->get_value( 'my_checkbox1' ));
3

In WooCommerce checkboxes have always a value of '1'.

So you do not need to pass 'value' => true: it does nothing.

To set checkbox checked or not WooCommerce uses the WP checked function where 1 (integer) is compared with the value you pass as third param in woocommerce_form_field.

Pass 1 as default and your checkbox will be checked as default.

$checked = $checkout->get_value( 'my_checkbox1' ) ? $checkout->get_value( 'my_checkbox1' ) : 1;

woocommerce_form_field( 'my_checkbox1', array( 
  'type' => 'checkbox', 
  'class' => array('input-checkbox'), 
  'label' => __('Standard Shipping (2–7 Days, FREE!) <span>Most items are shipped FREE OF CHARGE within Thailand.</span>'), 
  'required' => false,
), $checked );
  • would you know how to add Radio buttons to the check out page by any chance ? – dan Aug 1 '13 at 4:50
  • You cannot... Add checkboxes that is always checked using code above. Then add some javascript to prevent someone can check more than one checkbox whit same name. – gmazzap Aug 1 '13 at 5:16
3

I don't think G. M.'s answer is correct.

As he alluded to, I don't think it will make any difference what value you specify for the third parameter. (By the way, the 'value' element in the args array does not even exist - see http://docs.woothemes.com/wc-apidocs/source-function-woocommerce_form_field.html#1568-1787 )

What you need to do is set the 'default' value in the args array to 1. For example,

woocommerce_form_field( 'my_checkbox1', array( 
    'type' => 'checkbox', 
    'class' => array('input-checkbox'), 
    'label' => __('Standard Shipping (2–7 Days, FREE!) <span>Most items are shipped FREE OF CHARGE within Thailand.</span>'), 
    'required' => false, 
    'value'  => true, 
    'default' => 1 //This will pre-select the checkbox
), 'whatever');

Cheers, James

  • this is correct and works. the previous answer I did not test because it looked a bit bogus. – Eolis Feb 22 '18 at 23:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.