Skip to content

Instantly share code, notes, and snippets.

@NateWr
Created May 15, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NateWr/ac01564c40352c99bf24 to your computer and use it in GitHub Desktop.
Save NateWr/ac01564c40352c99bf24 to your computer and use it in GitHub Desktop.
Automatically block bookings when a max per-day booking limit is hit.
<?php
/**
* Plugin Name: Max Bookings for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Modifies the Restaurant Reservations plugin to rejects bookings if a max bookings limit has been reached for the day.
* Version: 1.0
* Author: Theme of the Crop
* Author URI: http://themeofthecrop.com
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: max-capacity-for-rtb
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
if ( !class_exists( 'mxbfrtbInit' ) ) {
class mxbfrtbInit {
/**
* The single instance of this class
*/
private static $instance;
/**
* Path to the plugin directory
*/
static $plugin_dir;
/**
* URL to the plugin
*/
static $plugin_url;
/**
* Maximum bookings per day
*/
public $max_bookings = 2;
/**
* Create or retrieve the single instance of the class
*
* @since 0.1
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new mxbfrtbInit;
self::$plugin_dir = untrailingslashit( plugin_dir_path( __FILE__ ) );
self::$plugin_url = untrailingslashit( plugin_dir_url( __FILE__ ) );
self::$instance->init();
}
return self::$instance;
}
/**
* Initialize the plugin and register hooks
*/
public function init() {
if ( !defined( 'RTB_BOOKING_POST_TYPE' ) ) {
return;
}
add_action( 'init', array( $this, 'load_textdomain' ) );
// Validate form fields
add_action( 'rtb_validate_booking_submission', array( $this, 'validate_booking_submission' ) );
}
/**
* Load the plugin textdomain for localistion
* @since 0.0.1
*/
public function load_textdomain() {
load_plugin_textdomain( 'max-bookings-for-rtb', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" );
}
/**
* Validate the booking submission to block bookings if max
* bookings reached
*/
public function validate_booking_submission( $booking ) {
$party = absint( $booking->party );
// Check if we have space for this booking
$capacity = $this->get_day_capacity( $booking->date );
if ( $capacity === 0 ) {
$booking->validation_errors[] = array(
'field' => 'date',
'error_msg' => 'Booked to capacity',
'message' => __( 'Sorry, we are fully booked that day. Please visit us another day.', 'max-bookings-for-rtb' ),
);
}
}
/**
* Get the remaining capacity for a given day
*/
public function get_day_capacity( $date ) {
$date = new DateTime( $date );
$bookings = new WP_Query(
array(
'posts_per_page' => 1000, // large upper limit
'post_type' => RTB_BOOKING_POST_TYPE,
'post_status' => 'confirmed',
'date_query' => array(
array(
'year' => $date->format( 'Y' ),
'month' => $date->format( 'm' ),
'day' => $date->format( 'd' )
)
)
)
);
if ( !$bookings->have_posts() ) {
return $this->max_bookings;
}
$capacity = $this->max_bookings - $bookings->found_posts;
return max( $capacity, 0 );
}
}
} // endif;
/**
* This function returns one mxbfrtbInit instance everywhere
* and can be used like a global, without needing to declare the global.
*
* Example: $ssfrtb = mxbfrtbInit();
*/
if ( !function_exists( 'mxbfrtbInit' ) ) {
function mxbfrtbInit() {
return mxbfrtbInit::instance();
}
add_action( 'plugins_loaded', 'mxbfrtbInit' );
} // endif;
@nikitae
Copy link

nikitae commented Sep 3, 2015

Is it possible to change this to max-bookings per time instead of date, and how do I add this plugin to restaurant_reservation part?

@benzdumol
Copy link

Hi Nate,
Just like what nikitae asked above, I was wondering if it's possible to change the max bookings per time instead of date?
Hoping to hear from you soon!
Thank you in advance.
Cheers,
Benessa

@ankitkumarsah
Copy link

What I am trying to do is to create a simple "hotel booking" with allotments per day. So, basically: The user chooses a Checkin and a Checkout Date and Hotel A or Hotel B.

Now, for each day between Checkin and Checkout there are allotments, i.e. we have the following possible dates with respective allotments (rooms available):

Hotel A:
17.10.2014 - 25 rooms
18.10.2014 - 40 rooms
19.10.2014 - 20 rooms

Hotel B:
17.10.2014 - 35 rooms
18.10.2014 - 50 rooms
19.10.2014 - 40 rooms
When booking the user first chooses Checkin and Checkout, then the Hotel and THEN the script should check if any of the days in between is fully booked for that particular hotel and that particular date range and return a message that there are no free allotments for the selected date range - maybe also show which days are "problematic" (i.e. fully booked) so that the user can choose a day before or after.

What is the best approach to achieve this? I have checked several DB schemas for this and table structures, but as I said I am only finding other solutions, not exactly fit for this problem.

sir please help me

@NateWr
Copy link
Author

NateWr commented Dec 22, 2020

Hi @ankitkumarsah, I'm no longer involved with the Restaurant Reservations plugin. You may want to contact the current plugin maintainers at https://wordpress.org/plugins/restaurant-reservations/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment