Woocommerce checkout page custom checkbox ticked

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' ));

2 Answers
2

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 );

Leave a Comment