Appointment + with limited booking amount

Hi,

I'm working with the appointments + plugin. I currently have a system set-up whereby there are two providers. for parties. The two parties are Farm Parties and Pony Parties.

As it stands, three parties can be booked per day. Two Farm parties and one Pony Party. As the location can only hold a maximum of three parties per day, I need to restrict the number of parties booked, and limit it to two per day. So if a pointy party has been booked - only one Farm Party will be available, and vice versa.

I hope this makes sense, and that some of you out there may have a solution for me.

Thanks in advance.

  • calvinrogercanas

    Hey there @Digital,

    Hope you're well! :slight_smile:

    You mean you want the Two Farm parties to share to same time-slot? If so, you need to setup one of the service provider as dummy. For more info about this read this: https://premium.wpmudev.org/forums/topic/how-to-allow-two-services-to-be-booked-in-the-same-time-spot#post-398590

    If you don't mean that, please let us know and we are happy to assist you.

    Looking forward for your reply! :slight_smile:

    Best Regards,
    Calvin

  • Ash

    Hello @Digital

    I hope you are well today.

    How many slots you have in a day? Do you mean a slot can be booked for three parties or a day? If a day, that's pretty complex and you need to customize the plugin.

    If slot, then -

    So if a pointy party has been booked - only one Farm Party will be available, and vice versa.

    This won't be possible out of the box. You have to select which one can be booked twice and which one can be booked once. Please let us know, we may find a workaround on this.

    Cheers
    Ash

  • Jose

    Hi there Digital,

    Hope you are doing fantastic today.

    This is something that we normally would skip and suggest you to hire a dev, since it is a specific customization and hence it is beyond our scope in the support crew.

    That said, I made a little exception and coded something for you. Not sure if it is exactly what you need, but it would serve as base if you want to tweak it or maybe hire someone to improve it.

    Basically, this will add a limit per day for each provider/service. So, if you have 2 different services, you will want to set the $limit parameter to 1. That way, users will be able to book only one appointment per day for each service.

    You can paste this code into an mu-plugin ir directly in your theme's functions.php:

    if(! class_exists('app_limit_per_day') ):
    
        class app_limit_per_day{
    
            function __construct(){
                add_filter('app_is_holiday', array($this, 'limit_appointments_per_day'), 20, 5);
            }
    
            function limit_appointments_per_day($is_holiday, $ccs, $cce, $service, $w){
                global $appointments;
    
                $limit = 1;//Change this parameter as needed.
                $count = 0;
                $week= date( "W", $ccs );
    
                $apps = $appointments->get_reserve_apps($appointments->location,$service, $appointments->worker, $week);
    
                if(!empty($apps)){
                    foreach($apps as $key => $app){
                        $app_start_time = strtotime($app->start);
                        if($ccs < $app_start_time && $cce > $app_start_time){
                            //The appointment is booked this day. Increase the counter.
                            $count ++;
                        }
                    }
                }
    
                $is_holiday = !($count < $limit);
    
                return $is_holiday;
            }
    
         }
        new app_limit_per_day;
    endif;

    Hope this helps :slight_smile:

    Cheers,
    Jose

  • Digital

    Jose,

    Wow! Thank you so much for the code - I really appreciate it!

    I do have one question however. You mentioned in you last email that the code you supplied will limit the number of bookings per supplier. Thats the tricky bit, because I have two suppliers (which you know). One who can allow two parties per day and the other who can allow one per day.

    I only want a maximum of two per day - so one supplier could supply both parties. Or both suppliers could provide one. It depends which slots gets booked first.

    I think what I would need is something that could limit the number of parties per day - regardless of suppliers. Would the code you supplied still work in this case?

    I hope that made sense.

    Thanks you so much once again - your support has been exceptional.

  • Jose

    Hi Digital,

    I haven't had the time to test -as I said before, this is beyond our scope- but this code should work:

    if(! class_exists('app_limit_per_day') ):
    
        class app_limit_per_day{
    
            function __construct(){
                add_filter('app_is_holiday', array($this, 'limit_appointments_per_day'), 20, 5);
            }
    
            function limit_appointments_per_day($is_holiday, $ccs, $cce, $service, $w){
                global $appointments;
    
                $limit = 2;//Change this parameter as needed.
                $count = 0;
                $week= date( "W", $ccs );
    
                $apps = $this->get_reserve_apps($appointments->location, $service, $week);
    
                if(!empty($apps)){
                    foreach($apps as $key => $app){
                        $app_start_time = strtotime($app->start);
                        if($ccs < $app_start_time && $cce > $app_start_time){
                            //The appointment is booked this day. Increase the counter.
                            $count ++;
                        }
                    }
                }
    
                $is_holiday = !($count < $limit);
    
                return $is_holiday;
            }
    
            function get_reserve_apps( $l, $s, $w, $week=0 ) {
                $apps = wp_cache_get( 'reserve_apps_'. $l . '_' . $s . '_' . $week );
                if ( false === $apps ) {
                    $location = $l ? "location='" . $this->db->escape($location) . "' AND" : '';
                        $apps = $this->db->get_results($this->db->prepare(
                                "SELECT * FROM {$this->app_table} " .
                                "WHERE {$location} service=%d" .
                                "AND (status='pending' OR status='paid' OR status='confirmed' OR status='reserved' OR status='completed') AND WEEKOFYEAR(start)=%d",
                                $s, $week)
                        );
                    wp_cache_set( 'reserve_apps_'. $l . '_' . $s . '_' . $week, $apps );
                }
                return $apps;
            }
    
        }
        new app_limit_per_day;
    endif;

    I'm assuming here that the service is the same for both providers. If that's not the case, then you would need to remove all the reference to service ($s) in the SQL query above, :wink:

    Cheers!
    Jose

  • Jose

    Hey Digital,

    Just tried on my end and didn't get that error. Make sure to not paste the code inside another class definition or function.

    Nevertheless, there were some errors in the code.
    This version works fine:

    if(! class_exists('app_limit_per_day') ):
    
        class app_limit_per_day{
    
            function __construct(){
                add_filter('app_is_holiday', array($this, 'limit_appointments_per_day'), 20, 5);
            }
    
            function limit_appointments_per_day($is_holiday, $ccs, $cce, $service, $w){
                global $appointments;
    
                $limit = 2;//Change this parameter as needed.
                $count = 0;
                $week= date( "W", $ccs );
    
                $apps = $this->get_reserve_apps($appointments->location, $service, $week);
    
                if(!empty($apps)){
                    foreach($apps as $key => $app){
                        $app_start_time = strtotime($app->start);
                        if($ccs < $app_start_time && $cce > $app_start_time){
                            //The appointment is booked this day. Increase the counter.
                            $count ++;
                        }
                    }
                }
    
                $is_holiday = !($count < $limit);
    
                return $is_holiday;
            }
    
            function get_reserve_apps( $l, $s, $w, $week=0 ) {
                global $wpdb, $appointments;
                $apps = wp_cache_get( 'reserve_apps_'. $l . '_' . $s . '_' . $week );
                if ( false === $apps ) {
                    $location = $l ? "location='" . $wpdb->esc_sql($l) . "' AND" : '';
                        $apps = $wpdb->get_results($wpdb->prepare(
                                "SELECT * FROM {$appointments->app_table}" .
                                " WHERE {$location} service=%d" .
                                " AND (status='pending' OR status='paid' OR status='confirmed' OR status='reserved' OR status='completed')",
                                $s)
                        );
                    wp_cache_set( 'reserve_apps_'. $l . '_' . $s . '_' . $week, $apps );
                }
                return $apps;
            }
    
        }
        new app_limit_per_day;
    endif;

    Cheers,
    Jose

  • Digital

    Hi Jose,

    Gotcha - so this is the query I should be using:

    `if(! class_exists('app_limit_per_day') ):

    class app_limit_per_day{

    function __construct(){
    add_filter('app_is_holiday', array($this, 'limit_appointments_per_day'), 20, 5);
    }

    function limit_appointments_per_day($is_holiday, $ccs, $cce, $service, $w){
    global $appointments;

    $limit = 2;//Change this parameter as needed.
    $count = 0;
    $week= date( "W", $ccs );

    $apps = $this->get_reserve_apps($appointments->location, $service, $week);

    if(!empty($apps)){
    foreach($apps as $key => $app){
    $app_start_time = strtotime($app->start);
    if($ccs < $app_start_time && $cce > $app_start_time){
    //The appointment is booked this day. Increase the counter.
    $count ++;
    }
    }
    }

    $is_holiday = !($count < $limit);

    return $is_holiday;
    }

    function get_reserve_apps( $l, $s, $w, $week=0 ) {
    global $wpdb, $appointments;
    $apps = wp_cache_get( 'reserve_apps_'. $l . '_' . $s . '_' . $week );
    if ( false === $apps ) {
    $location = $l ? "location='" . $wpdb->esc_sql($l) . "' AND" : '';
    $apps = $wpdb->get_results(
    "SELECT * FROM {$appointments->app_table}" .
    " WHERE {$location}" .
    " (status='pending' OR status='paid' OR status='confirmed' OR status='reserved' OR status='completed')"
    ); wp_cache_set( 'reserve_apps_'. $l . '_' . $s . '_' . $week, $apps );
    }
    return $apps;
    }

    }
    new app_limit_per_day;
    endif;

  • Jose

    You are inserting the code in a wrong place.
    Make sure that it is not wrapped by {}.

    If you are unsure, you can remove the code from functions.php and create an empty file called something like app_limit_per_day.php in the following location:
    ../wp-content/mu-plugins/ (create the folder if it doesn't exists).

    In that file, just paste the code above, adding <?php in the first line. :slight_smile:

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.