I'm attempting to create a conditional checkbox that shows/hides based on the shipping and billing state of the user during checkout. Currently, I can't find a way to conditionally show this information. All I need is for the checkbox to appear when the user is checking out from California. I also have a warning text box that I need to appear based on State and was able to make this work. For some reason, I can't seem to have the checkbox ONLY show up when California is selected as the state.
I've tried using the script that's allowing the text warning to show/hide, but it isn't working for the checkbox.
**/ add_action('woocommerce_after_checkout_form', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my-new-field"><h3>'.__(' ').'</h3>';
woocommerce_form_field( '', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, I Understand.'),
'required' => true,
), $checkout->get_value( '' ));
echo '</div>';
}
/** * Process the checkout **/ add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() { global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['my_checkbox'])
$woocommerce->add_error( __('Please agree to my checkbox.') );
}
// First Step // Create Prop 65 warning message and hook it into WooCommerce After Order Review // "display:none" we have hidden the warning message by default
add_action( 'woocommerce_after_checkout_form', 'show_prop_message' );
function show_prop_message() { echo 'placeholderforpicture" />WARNING SOME PRODUCTS SOLD ON THIS STORE WEBSITE CAN CONTAIN CHEMICALS KNOWN TO THE STATE OF CALIFORNIA TO CAUSE CANCER, BIRTH DEFECTS OR OTHER REPRODUCTIVE HARM. Details
// Second Step // Show or hide warning message based on billing and shipping state // First trigger is fired on billing state selection in case the "Ship to a different address" checkbox is unselected // Second trigger is fired if the "Ship to a different address" checkbox is ticked // Initally the "display:none" hides the warning message by default
add_action( 'woocommerce_before_checkout_form', 'show_warning_message' );
function show_warning_message(){
?>
<script>
jQuery(document).ready(function($){
// Set the shipping state 2 char code (This selection will fire the warning message display)
// Fires secondary if shipping state is set to California
var stateCode = 'CA';
$('select#shipping_state').change(function(){
selectedState = $('select#shipping_state').val();
if( selectedState == stateCode ){
$('.prop65-warning').show();
}
else {
$('.prop65-warning').hide();
}
});
});
</script>
<script>
jQuery(document).ready(function($){
// Set the billing state 2 char code (This selection will fire the warning message display)
// Fires initially if billing state is California
var stateCode = 'CA';
$('select#billing_state').change(function(){
selectedState = $('select#billing_state').val();
if( selectedState == stateCode ){
$('.prop65-warning').show();
}
else {
$('.prop65-warning').hide();
}
});
});
</script>