PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Old April 21st, 2016, 05:49 AM
EFitzpatr7 EFitzpatr7 is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2016
Location: Stoke-on-Trent, United Kingdom
Posts: 1 EFitzpatr7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 37 sec
Reputation Power: 0
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 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,1);?>


to

PHP Code:
<?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.

Reply With Quote
Old January 7th, 2017, 05:39 PM
icandothat's Avatar
icandothat icandothat is offline
Super Moderator
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,733 icandothat User rank is Private First Class (20 - 50 Reputation Level)icandothat User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 10 h 45 m 8 sec
Reputation Power: 11
you're marking every option as checked. Only the last one show up as checked because radio buttons only allow one of the collection to be checked. so, the last one is the one that reflects the check.

To check a specific one by default use a conditional statement. something like



<?php echo sanitize_title( $option['label']);?>" <?php
if($current_value==0){
checked( $current_value,1);
}else{
checked( $current_value,1);

}
?>
__________________
There is no spoon.

Last edited by icandothat : January 7th, 2017 at 05:40 PM. Reason: poor formating

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Woocommerce Product Add-ons - Auto-select first option


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap