NateWr / restaurant-reservations

WordPress plugin to accept restaurant reservations online
68 stars 51 forks source link

Early bookings for more than a day checks time as well #141

Open almiteyG opened 6 years ago

almiteyG commented 6 years ago

This maybe more of an enhancement than a bug. But in a real world scenario, you will not expect a customer to think like this unless you put detailed error messages.

Below is the scenario: I have set early bookings for 14 days. Say for example today is first of the month and the time is 15:00.

When i try to make a booking for the 14th of the month for any time period after 15:00 - it will not allow me. Now technically this is correct because every hour after 3pm on the 14th day is outside the 14 day window.

Now either the time picker should be smart enough to disable those times or the form should submit for all valid times on the 14th day.

I have introduced a new early_request variable format that checks against the date only instead of date and time. Then for all early bookings greater than a day it checks for day difference only. Another alternative could be to edit the error message to say 'Sorry, bookings can be made only till (current time) on (date today + early bookings)'

    // Check against valid open dates/times
    if ( is_object( $time ) && is_object( $date ) ) {

        $request = new DateTime( $date->format( 'Y-m-d' ) . ' ' . $time->format( 'H:i:s' ) );

        $early_request = new DateTime( $date->format( 'Y-m-d' ) );

        // Exempt Bookings Managers from the early and late bookings restrictions
        if ( !current_user_can( 'manage_bookings' ) ) {

            $early_bookings = $rtb_controller->settings->get_setting( 'early-bookings' );

            if ( !empty( $early_bookings ) ) {
                $early_bookings_seconds = $early_bookings * 24 * 60 * 60; // Advanced bookings allowance in seconds

                if ( $early_bookings <= 1 ) {
                    //Orignal IF by NateWr
                                          if ( $request->format( 'U' ) > ( current_time( 'timestamp' ) + $early_bookings_seconds ) ) {
                        $this->validation_errors[] = array(
                            'field'     => 'time',
                            'error_msg' => 'Booking request too far in the future',
                            'message'   => sprintf( __( 'Sorry, bookings can not be made more than %s days in advance.', 'restaurant-reservations' ), $early_bookings ),
                        );
                    }                   
                } 
                else {
                    if ( $early_request->format( 'U' ) > ( current_time( 'timestamp' ) + $early_bookings_seconds ) ) {
                        $this->validation_errors[] = array(
                            'field'     => 'time',
                            'error_msg' => 'Booking request too far in the future',
                            'message'   => sprintf( __( 'Sorry, bookings can not be made more than %s days in advance.', 'restaurant-reservations' ), $early_bookings ),
                        );
                    }
                }
            }