• Resolved daymobrew

    (@daymobrew)


    I have a multicheck which I populate with the dates of the next 7 days e.g.
    25-04-2020 => Saturday 25 April 2020
    26-04-2020 => Sunday 26 April 2020
    27-04-2020 => Monday 27 April 2020
    etc

    I would like to always have Monday and Tuesday checked.
    How can I do that?
    I tried using a ‘default_cb’ callback but didn’t know what to return.

    add_action( 'cmb2_admin_init', 'sab_delivery_dates_availability' );
    function sab_delivery_dates_availability() {
    	$cmb_options = new_cmb2_box( array(
    		'id'            => 'delivery_dates',
    		'title'         => 'Delivery Dates Availability',
    		'object_types' => array( 'options-page' ),
    		'option_key'      => 'delivery_dates', // The option key and admin menu page slug.
    		'icon_url'        => 'dashicons-calendar-alt', // Menu icon. Only applicable if 'parent_slug' is left empty.
    		'capability'      => 'edit_posts', // Capability required to view this options page.
    		'position'        => 2, // Menu position. Only applicable if 'parent_slug' is left empty.
    		'save_button'     => 'Save Dates',
    	) );
    
    	// Create the checkbox list of dates.
    	global $default_disable;
    	// The index is the date (dd-mm-yyyy format) and the checkbox text is a readable date for useability.
    	$today = time();
    	$dates_array = array();
    	$secs_in_a_day = 60 * 60 * 24;
    	for ( $i = 0; $i < 7; $i++ ) {
    		$time = $today + ( $i * $secs_in_a_day );  // Add $i days to time.
    		$date_info = getdate( $time );
    		// Index is dd-mm-yyyy (for DatePicker date exclusion) and then the readable date.
    		$date_index = sprintf( '%02d-%02d-%d', $date_info['mday'], $date_info['mon'], $date_info['year'] );
    		$dates_array[ $date_index ] = sprintf( '%s %d %s %d', $date_info['weekday'], $date_info['mday'], $date_info['month'], $date_info['year'] );
    	}
    
    	$cmb_options->add_field( array(
    		'desc' => 'Check dates that do <strong>NOT</strong> have delivery slots available.',
    		'type' => 'multicheck',
    		'id' => 'dates',
    		'select_all_button' => false,  // Exclude the "Select All" button.
    		'options' => $dates_array,
    	) );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I wonder if something with these common parameters could be used https://github.com/CMB2/CMB2/wiki/Field-Parameters#default

    Otherwise, perhaps a touch of jQuery to auto-check things for you?

    Since this looks to be for an options page, getting the saved value updated is looking to be an interesting challenge.

    Thread Starter daymobrew

    (@daymobrew)

    Thanks for the reply.
    I decided against pre-setting the data as it would have negative knock-on effects if the client didn’t save new dates each week.

    The site is using jQuery DatePicker on the front end and I was feeding these dates as those to disable. I have changed my PHP code (that generates JavaScript code) to generate the dates for the next 4 Mondays and Tuesdays. This saves a lot of complicated stuff at the CMB2 side.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Cool, glad you found a solution that’ll work for you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How do precheck some items in multicheck?’ is closed to new replies.