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

Related posts

2 comments

  1. 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 );
    
  2. 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

Comments are closed.