Page 1 of 1

Woocommerce Product Add-ons - Auto-select first option

Posted: Mon Apr 18, 2016 6:59 am
by EFitzpatr7
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:

Code: Select all

<?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

Code: Select all

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

Code: Select all

<?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.

Re: Woocommerce Product Add-ons - Auto-select first option

Posted: Mon Apr 18, 2016 1:03 pm
by Christopher
I think you need to understand what the parameters for checked() are. The first parameter is the value for the current checkbox. The second parameter is the value of the checkbox in the set that should be checked. So you should determine $checked_value before the loop and pass it as the second parameter.

Re: Woocommerce Product Add-ons - Auto-select first option

Posted: Thu Apr 21, 2016 4:13 am
by EFitzpatr7
Christopher wrote:I think you need to understand what the parameters for checked() are. The first parameter is the value for the current checkbox. The second parameter is the value of the checkbox in the set that should be checked. So you should determine $checked_value before the loop and pass it as the second parameter.
Have you got any information on the checked() parameters, or any advice on what parameter I should be using in order to select the first radio button?

Re: Woocommerce Product Add-ons - Auto-select first option

Posted: Thu Apr 21, 2016 1:49 pm
by Christopher
According to the documentation, you should set the value of the second parameter to the value of the first radio button to have it selected. I searched for "woocommerce checked() function and this was the first result:

http://woocommerce.wp-a2z.org/oik_api/checked/