wp-ap
wp-ap

Reputation: 109

WooCommerce radio option checked by default

Here is the answer for checkbox but I cannot find it for radio button.

For example I have 2 options radio and I want to make checked the first option by default.

'type'  => 'radio',
'class' => array('box form-row-wide'),
'options' => array(
    'default'  => 'Default option',
    'other'    => 'Other option',
  ),

Where to add $checked so the first option (Default option) will be checked by default?

Thanks.

Upvotes: 0

Views: 885

Answers (3)

jallard2022
jallard2022

Reputation: 1

Set the default value with a condition in the 'value' field

    woocommerce_wp_radio( array(
        'value'   => get_post_meta( $post_id, '_foo', true ) ?: 'three',
        'options' => array(
            'one'    => 'Option one',
            'two'     => 'Option two',
            'three'     => 'Option three',
        )
    ) );

Upvotes: 0

63N
63N

Reputation: 133

I did a lot of Googling to find this out and the other answer here helped but did not work for me. I had to use the radio label value, not the input name, like so:

array(
    'type'        => 'radio',
    'class'       => array( 'form-row-wide' ),
    'options'     => array(
        'one'        => 'Option one',
        'two'        => 'Option two',
        'three'      => 'Option three',
    ),
    'default'     => 'Option one',
),

Upvotes: 0

wp-ap
wp-ap

Reputation: 109

I just figured it out. Here is the answer.

'type'  => 'radio',
'class' => array('box form-row-wide'),
'options' => array(
'default1'  => 'Default option',
'other2'    => 'Other option',
),
'default' => 'default1' ,

So it is 'default' => 'option you want to be checked',

Upvotes: 1

Related Questions