Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created December 9, 2022 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/9e5bd9f945efdbb4bc18d5384186a412 to your computer and use it in GitHub Desktop.
Save plugin-republic/9e5bd9f945efdbb4bc18d5384186a412 to your computer and use it in GitHub Desktop.
<?php
/**
* JS function to dynamically remove options from an add-ons select field
* Update the field ID of your add-ons select field in line 12 below
*/
function prefix_update_time_field() {
?>
<script>
jQuery( document ).ready( function( $ ) {
// This is the ID of the select field listing the possible times
var field_id = 911;
var field = $( '.pewc-field-' + field_id ).find( 'select' );
var first_option = $( field ).find( "option:first-child" ).text();
// This is a list of the available times per day
function get_times( day ) {
var times = [
[ '10:00' ], // Sunday
[ '10:00', '14:00', '17:00' ], // Monday
[ '17:00' ], // Tuesday
[ '10:00', '14:00', '17:00' ], // Wednesday
[ '17:00' ], // Thursday
[ '10:00', '14:00', '17:00' ], // Friday
[ '10:00' ] // Saturday
];
return times[day];
}
$( ".bfws-start-date" ).on( 'change', function() {
var date = $(this).datepicker('getDate');
var day_of_week = date.getUTCDay();
var times = get_times( day_of_week );
// Remove all options from time field except first option
$( field ).empty();
$( field ).append( '<option value="">' + first_option + '</option>' );
// Repopulate with new options
for( var i = 0; i < times.length; i++ ) {
$( field ).append( '<option value="' + times[i] + '">' + times[i] + '</option>' );
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'prefix_update_time_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment