Q: Woocommerce Product Add-ons - Auto-select first option The agency I work for are trying to get a client's website to auto-select the first option on the maintenance packages for their products.
Currently, we are using an if statement within a foreach.
The code below is the script we are currently using:
<?php foreach ( $addon['options'] as $i => $option ) :

                $price         = $option['price'] > 0 ? '(' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . ')' : '';
                $current_value = 0;

                if ( isset( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) ) {
                                $current_value = (
                                                                isset( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) &&
                                                                in_array( sanitize_title( $option['label'] ), $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] )
                                                                ) ? 1 : 0;
                }
                ?>

                <p class="form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon['field-name'] ) . '-' . $i; ?>">
                                <label><input type="radio" class="addon addon-radio" name="addon-<?php echo sanitize_title( $addon['field-name'] ); ?>[]" data-raw-price="<?php echo esc_attr( $option['price'] ); ?>" data-price="<?php echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?php echo sanitize_title( $option['label'] ); ?>" <?php checked( $current_value, 1 ); ?> /> <?php echo wptexturize( $option['label'] . ' ' . $price ); ?></label>
                </p>

<?php endforeach; ?>

The code above currently doesn't select any of the radio buttons, but if we change
<?php echo sanitize_title( $option['label'] ); ?>" <?php checked( $current_value, 1 ); ?>

to
<?php echo sanitize_title( $option['label'] ); ?>" <?php checked( $current_value, 0 ); ?>

the final radio point is selected.
The only issue we have then got is the fact that we will then have to manually go and change every single product, and move the option "None" to the end of the list, as it is currently the first option on all of the products.
Is there a way with the code above to be able to auto-select the first option rather than the last? The name for the first option is: addon-2491-maintenance-packages-0?
Any help is much appreciated.

A: Is this form only filled out once? Or can users be sent back to it, needing it to know what they have already selected?
If the former, it's not too difficult to select the option you want.
You just need to replace this:
<?php checked( $current_value, 1 ); ?>

with this:
<?php checked( '2491-maintenance-packages-0', sanitize_title( $option['label'] ) ); ?>

The checked() function simply returns the HTML checked='checked' when the two values sent to it are a match, thereby instructing the browser to render that option pre-checked. It is most often used not to pre-check a default option, but rather to ensure that when a user visits an options page, whatever the option is currently set to in the database is rendered checked. WordPress' checked() function is therefore simply syntactic sugar to save you having to fill your code with if statements.
The reason you only had the last option selected when you used $current_value, 0 was likely because every single option came to the form unselected, therefore $current_value was always equal to 0, therefore every option became selected... and because only one radio option can be selected at a time, the last one ends up overriding all the rest.
Now, it's important to note that your flow here is not necessarily intuitive. I'm not sure how the rest of your site works, but as I hinted at the top of this answer, if a user needs to come to this page with whatever they have selected being already pre-checked, you're going to need to modify your flow. Otherwise their own settings will be overridden by your 'default option' once this form is saved.
