Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Woocommerce Product Add-ons - Auto-select first option
#1
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 Code:
<?php foreach ( $addon['options'] as $i => $option ) :

 
               $price         $option['price'] > '(' wc_priceget_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_arraysanitize_title$option['label'] ), $_POST'addon-' sanitize_title$addon['field-name'] ) ] )
 
                                                               ) ? 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); ?> /> <?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 Code:
<?php echo sanitize_title$option['label'] ); ?><?php checked$current_value); ?>

to

PHP Code:
<?php echo sanitize_title$option['label'] ); ?><?php checked$current_value); ?>

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  My ip logger with email and html logging option ELY_M 7 3,013 12-13-2010, 10:41 PM
Last Post: Bursihido
  Auto Ban IP's & Host's zone 2 990 11-06-2009, 09:15 PM
Last Post: zone
  [PHP] Login && Register With Remember Me Option [mySQL] Bartdevil 18 6,242 10-26-2009, 07:54 AM
Last Post: Bartdevil

Forum Jump:


Users browsing this thread: 1 Guest(s)