-
-
Save NateWr/5af0297b1fb9d6d75ece to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Plugin Name: Max Capacity for Restaurant Reservations | |
* Plugin URI: http://themeofthecrop.com | |
* Description: Modifies the Restaurant Reservations plugin to rejects bookings if a max capacity 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( 'mxcfrtbInit' ) ) { | |
class mxcfrtbInit { | |
/** | |
* 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 capacity per night | |
*/ | |
public $max_capacity = 12; | |
/** | |
* Create or retrieve the single instance of the class | |
* | |
* @since 0.1 | |
*/ | |
public static function instance() { | |
if ( !isset( self::$instance ) ) { | |
self::$instance = new mxcfrtbInit; | |
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-capacity-for-rtb', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" ); | |
} | |
/** | |
* Validate the booking submission to block bookings if max | |
* capacity reached | |
*/ | |
public function validate_booking_submission( $booking ) { | |
$party = absint( $booking->party ); | |
$booking_id = isset( $booking->ID ) ? $booking->ID : null; | |
// Check if we have space for this booking | |
$capacity = $this->get_day_capacity( $booking->date, $booking_id ); | |
if ( $capacity === 0 ) { | |
$booking->validation_errors[] = array( | |
'field' => 'party', | |
'error_msg' => 'Booked to capacity', | |
'message' => __( 'Sorry, we are fully booked that day. Please visit us another day.', 'max-capacity-for-rtb' ), | |
); | |
} elseif ( $party > $capacity ) { | |
$booking->validation_errors[] = array( | |
'field' => 'party', | |
'error_msg' => 'Booking request party is too big', | |
'message' => sprintf( __( 'Sorry, we only have space for %s more people on that day. Please visit us another day.', 'max-capacity-for-rtb' ), $capacity ), | |
); | |
} | |
} | |
/** | |
* Get the remaining capacity for a given day | |
*/ | |
public function get_day_capacity( $date, $exclude_booking_id = null ) { | |
$date = new DateTime( $date ); | |
$args = array( | |
'post_type' => RTB_BOOKING_POST_TYPE, | |
'post_status' => 'confirmed', | |
'posts_per_page' => -1, | |
'date_query' => array( | |
array( | |
'year' => $date->format( 'Y' ), | |
'month' => $date->format( 'm' ), | |
'day' => $date->format( 'd' ) | |
) | |
) | |
); | |
if ( $exclude_booking_id ) { | |
$args['post__not_in'] = array( $exclude_booking_id ); | |
} | |
$bookings = new WP_Query( $args ); | |
if ( !$bookings->have_posts() ) { | |
return $this->max_capacity; | |
} | |
$capacity = $this->max_capacity; | |
while ( $bookings->have_posts() ) { | |
$bookings->the_post(); | |
$post_meta = get_post_meta( $bookings->post->ID, 'rtb', true ); | |
if ( !empty( $post_meta['party'] ) ) { | |
$capacity -= $post_meta['party']; | |
} | |
} | |
return max( $capacity, 0 ); | |
} | |
} | |
} // endif; | |
/** | |
* This function returns one mxcfrtbInit instance everywhere | |
* and can be used like a global, without needing to declare the global. | |
* | |
* Example: $ssfrtb = mxcfrtbInit(); | |
*/ | |
if ( !function_exists( 'mxcfrtbInit' ) ) { | |
function mxcfrtbInit() { | |
return mxcfrtbInit::instance(); | |
} | |
add_action( 'plugins_loaded', 'mxcfrtbInit' ); | |
} // endif; |
Hi Nate,
I hope all is well and I hope you'll see this message.
I am using the Restaurant Reservations plugin and it's awesome. The client that I'm servicing wants to add location, so used the Business Profile plugin for that based on your recommendation and it's working great as well. Then, the client wants to have max capacity checker and thankfully you have it as well. The only downside is that I can't customize the max capacity for every location. So I need you help here on how to create max capacity every location. It will be very helpful if you can show me how to do it.
Hoping to hear from you soon!
Thank you in advance.
Cheers,
Benessa
Hi there, I have to add my 2cents here.
A Chef works in shifts, for lunch and then for dinner. The restaurant has a capacity of 30 seats. 30 seats at lunch, 30 seats at dinner.
If we set a limit to 60 seats, we could be over-booking at lunch while being empty at dinner ...
If we set a limit to 30 seats, we will be far below our capacity and will lose half of our money ...
So is there a way to set a limit not by restaurant, but by lunch time (morning/lunch/dinner) (and by restaurant if able) ?
I could pay for such a plugin.
Regards,
Ben
Hi benoitheylens,
I started a thread here: https://wordpress.org/support/topic/max-capacity-for-restaurant-reservations-for-every-location/ for my issue Max Capacity for Restaurant Reservations for Every Location. Please check it out maybe it can help. :)
~ Benessa
Hi benzdumol,
I've been looking to customise the plugin in a similar way as benoitheylens for a client, and have followed your thread with Nate. Unfortunately my knowledge of PHP isn't getting me very far and I've now hit a wall.
I don't suppose you'd be willing to share the final code edits you made to achieve your max capacity for multiple locations?
Many thanks in advance,
Chris
Seems to be not working. I added the plugin, activated it but i can book as much as i want.