1
votes

I have a checkout field and I want to add date ranges instead of picking a single date. Is it possible to use the snippets from jqueryUI to style it and make it a range of dates? I use this function in Woocommerce checkout page on Wordpress.

Below is my current code in the functions.php where I've added the date.

// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {

    // Only on front-end and checkout page
    if( is_admin() || ! is_checkout() ) return;

    // Load the datepicker jQuery-ui plugin script
    wp_enqueue_script( 'jquery-ui-datepicker' );
}

// Call datepicker functionality in your custom text field
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {

    date_default_timezone_set('America/Los_Angeles');
    $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));

    echo '<div id="my_custom_checkout_field">
    <h3>'.__('Check In Date').'</h3>';

    echo '
    <script>
        jQuery(function($){
            $("#datepicker").datepicker();
        });
    </script>';

   woocommerce_form_field( 'order_checkin_date', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'id'            => 'datepicker',
        'required'      => true,
        'label'         => __('Check-in Date'),
        'placeholder'       => __('Select Date'),
        'options'     =>   $mydateoptions
        ),$checkout->get_value( 'order_checkin_date' ));

    echo '</div>';
}
2

2 Answers

0
votes

This information is available in the jQueryUI documentation. Here's an example of restricting the user to only allow a date 20 days in the past and 1 month + 20 days into the future. Replace your current $("#datepicker").datepicker(); declaration with the following

<script>
  jQuery( function() {
    $( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
  } );
  </script>
0
votes

To enable a date range with jQuery-ui Datepicker, there is 2 ways:

  • With 2 imput text fields that will display a datepicker each with "From" and "To" dates range.
  • A unique inline date-picker with 2 hidden fields for "from" and "to" dates range, like in this answer.

Here Below based on jQuery-ui datepicker range official documentation, you will be able to set a date range in checkout:

// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {

    // Only on front-end and checkout page
    if( is_admin() || ! is_checkout() ) return;

    // Load the datepicker jQuery-ui plugin script
    wp_enqueue_script( 'jquery-ui-datepicker' );
}

// Call datepicker functionality in your custom text field
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {

    date_default_timezone_set('America/Los_Angeles');

    ?>
    <div id="my_custom_checkout_field">
    <h3><?php _e('Check In Date'); ?><abbr class="required" title="required">*</abbr></h3>
    <?php

   woocommerce_form_field( 'order_in_date', array(
        'type'          => 'text',
        'class'         => array('form-row-first'),
        'id'            => 'from',
        'required'      => true,
        'placeholder'       => __('Select Date in'),
    ),$checkout->get_value( 'order_in_date' ));

    woocommerce_form_field( 'order_out_date', array(
        'type'          => 'text',
        'class'         => array('form-row-last'),
        'id'            => 'to',
        'required'      => true,
        'placeholder'       => __('Select Date out'),
        'clear'         => true
    ),$checkout->get_value( 'order_out_date' ));

    ?>
    </div>

    <script>
        jQuery(function($){
            var dateFormat = "yy/mm/dd",
                from = $( "#from" ).datepicker().on( "change", function() {
                      to.datepicker( "option", "minDate", getDate( this ) );
                }),
                to = $( "#to" ).datepicker().on( "change", function() {
                    from.datepicker( "option", "maxDate", getDate( this ) );
                });

            function getDate( element ) {
                var date;
                try {
                    date = $.datepicker.parseDate( dateFormat, element.value );
                } catch( error ) {
                    date = null;
                }
                return date;
            }
        });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.