Okay
  Public Ticket #2599426
Events should not overlap
Closed

Comments

  •  1
    intel started the conversation

    Hej Zhivko,

    Is it possible to create a calendar so that events do not overlap?
    I wish the user does not have the option to select time if someone has already taken this time. As you can see in the picture below, 2 different users booked the same car. One is from 09-12 and another user chose the same car from 11-16. So both have booked the same car between 11-12 which is not acceptable.
    So I just want if one user selected from 9-12 another can not select that time and need to select from 12-16. 

  •  780
    Zhivko replied

    Hi,

    I wrote a code for a client some time ago that prevents users adding event if it overlaps.

    You can give it a try. Place the code in your active theme functions.php file

    add_filter('stec_insert_post', function($data) {
        if ($data['post_type'] !== 'stec_event') {
            return $data;
        }
        $id       = $data['ID'];
        $tz       = get_post_meta($data['meta_input']['calid'], 'timezone', true);
        $start_dt = new \DateTime($data['meta_input']['start_date'], new \DateTimeZone($tz));
        $end_dt   = new \DateTime($data['meta_input']['end_date'], new \DateTimeZone($tz));
        $start    = $start_dt->format('Y-m-d H:i:s');
        $end      = $end_dt->format('Y-m-d H:i:s');
        
        $occurences = \Stachethemes\Stec\Events::get_events_between($start, $end, $tz, array(), true);
        if ($occurences) {
            foreach ($occurences as $occ) {
                if ($id !== $occ->get_id()) {
                    $s1 = $start_dt->format('U');
                    $e1 = $end_dt->format('U');
                    $s2 = $occ->get_start_date('U');
                    $e2 = $occ->get_end_date('U');
                    
                    if ($e1 > $s2 || $s1 > $e2) {
                        throw new \Stachethemes\Stec\Stec_Exception('This datetime is closed');
                    }
                }
            }
        }
        return $data;
    });
    



    Stachethemes Developer

  •  1
    intel replied

    Hi Zhivko, 

    i tried that script but it seems it is not working 100%. 

    f.ex. i booked in calendar one date 9 mar. 2021 17:00:00  to 9 mar. 2021 19:00:00.

    If i try to book 9 mar. 2021 17:00:00  to 9 mar. 2021 19:00:00 it says that date time is closed. 

    But if i try 9 mar. 2021 18:00:00  to 9 mar. 2021 19:00:00. it let me book event. 

    Than i tried to find out what could be problem and i can see that variable 

    $s2 = $occ->get_start_date('U');

    is going one hour in plus. 

    So if set time to 18 and output variable it says that it is 19. How can we fix that? 

    I tried to change UTC time on my site, and on calendar and it doesn't help. 

  •  780
    Zhivko replied

    I think I've sent you the wrong solution...

    Please try the one I've attached here.


    Stachethemes Developer

  •  1
    intel replied

    Perfect Zhivko. 

    This is working great. 

    Thank you very much.