Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to book maximum x number of events per day - per week - per month per calendar #74

Open
MNRKLK opened this issue Jul 3, 2020 · 12 comments
Labels
Booking Activities For core Booking Activities plugin custom code Custom code provided as a temporary solution pending for votes Needs more votes to be validated or to increase priority

Comments

@MNRKLK
Copy link

MNRKLK commented Jul 3, 2020

Hello!
This is a great work that you have done here!
I would like to know if using a custom code or perhaps you could include this in an update if useful for others, to be able to block the users from booking more than x number of events per day, per week or per month per calendar
For instance: 1 user can maximum book 2 events a day for calendar A, the same user can maximum book 1 event a day for calendar B, the same user can maximum book 1 event a week for calendar C
This case could be useful in case of subscriptions and not letting people book all the events in a day.
Thank you!

@MNRKLK MNRKLK changed the title Allow users to book x numbers of events per day - per week - per month and not more per calendar Allow users to book maximum x number of events per day - per week - per month per calendar Jul 3, 2020
@yoancutillas
Copy link
Collaborator

yoancutillas commented Jul 6, 2020

Hello,
Thanks a lot!

This feature has been requested, I add your vote, thank you.

I will post a custom code here for reference (you will need to adapt it to your needs, and maintain it) using the bookacti_validate_booking_form hook.

The code below allows a logged-in user to book a maximum of 2 distinct events per month, and 10 bookings per month.

This code is to be added in your child theme functions.php or with Code Snippets:

EDIT:
2021-05-14 - Compatibility with Booking Activities 1.9 and more. Now works with groups of events. Count the bookings quantity instead of distinct bookings. Do not count events already booked.
2023-01-27 - Compatibility with Booking Activities 1.15.6 and more.

// Restrict the number of bookings / booked events per user per period (per month in that example)
function my_theme_restrict_number_of_bookings_per_period( $validated, $picked_events, $args ) {
	// Don't check if the booking is already refused 
	if( $validated[ 'status' ] !== 'success' ) { return $validated; }

	// Doesn't work for non-logged-in users
	$user_id = get_current_user_id();
	if( ! $user_id ) { return $validated; }

	/****** SET THE MAX VALUES HERE *****/
	$max_bookings_per_month = 10;
	$max_booked_events_per_month = 2;
	
	// Keep one entry per event, not per group
	$picked_events_single = bookacti_reconstruct_picked_events_array( $picked_events );
	
	// Check if each picked event can be booked according to the per-period restrictions
	foreach( $picked_events_single as $picked_event ) {
		// First day and last day of the picked event month
		$first_day_of_month = new DateTime( $picked_event[ 'start' ] );
		$first_day_of_month->modify( 'first day of this month' );
		$last_day_of_month = new DateTime( $picked_event[ 'start' ] );
		$last_day_of_month->modify( 'last day of this month' );

		/****** SET THE PERIOD HERE *****/
		$from = $first_day_of_month->format( 'Y-m-d' ) . ' 00:00:00';
		$to = $last_day_of_month->format( 'Y-m-d' ) . ' 23:59:59';

		$filters = bookacti_format_booking_filters( array(
			'templates' => false,
			'user_id' => $user_id,
			'from' => $from,
			'to' => $to,
			'active' => 1
		));
		$bookings = bookacti_get_bookings( $filters );
		
		// Count bookings and booked events
		$bookings_nb = 0;
		$booked_events_uid = array();
		if( $bookings ) {
			foreach( $bookings as $booking ) {
				$bookings_nb += $booking->quantity;
				$booked_events_uid[] = $booking->event_id . '_' . $booking->event_start . '_' . $booking->event_end;
			}
		}
		
		// Add picked events to booked events count
		$from_dt = new DateTime( $from );
		$to_dt = new DateTime( $to );
		$picked_events_uid = array();
		foreach( $picked_events_single as $future_booked_event ) {
			$future_booked_event_start_dt = new DateTime( $future_booked_event[ 'start' ] );
			if( $future_booked_event_start_dt >= $from_dt && $future_booked_event_start_dt <= $to_dt ) {
				$bookings_nb += $args[ 'quantity' ];
				$picked_events_uid[] = $future_booked_event[ 'id' ] . '_' . $future_booked_event[ 'start' ] . '_' . $future_booked_event[ 'end' ];
			}
		}
		// Count only distinct events (if the event was already booked, do not count it)
		$booked_events_uid = array_unique( array_diff( array_merge( $booked_events_uid, $picked_events_uid ), array_intersect( $booked_events_uid, $picked_events_uid ) ) );
		
		// Check if the max number of bookings is reached
		if( $bookings_nb > $max_bookings_per_month ) {
			$validated[ 'status' ] = 'failed';
			$validated[ 'error' ] = 'too_many_bookings_per_month';
			$validated[ 'messages' ][ 'too_many_bookings_per_month' ] = array( 'You can make only ' . $max_bookings_per_month . ' bookings per month.' );
			break;
		}
		
		// Check if the max number of events is reached
		$booked_events_nb = count( $booked_events_uid );
		if( $booked_events_nb > $max_booked_events_per_month ) {
			$validated[ 'status' ] = 'failed';
			$validated[ 'error' ] = 'too_many_booked_events_per_month';
			$validated[ 'messages' ][ 'too_many_booked_events_per_month' ] = array( 'You can book only ' . $max_booked_events_per_month . ' distinct events per month.' );
			break;
		}
	}

	return $validated;
}
add_filter( 'bookacti_validate_picked_events', 'my_theme_restrict_number_of_bookings_per_period', 100, 3 );

@yoancutillas yoancutillas added Booking Activities For core Booking Activities plugin custom code Custom code provided as a temporary solution pending for votes Needs more votes to be validated or to increase priority labels Jul 6, 2020
@MNRKLK
Copy link
Author

MNRKLK commented Jul 17, 2020

Hi!!
Thank you very much! I'll implement it and will let you know.
Kind Regards,

@prosie
Copy link

prosie commented Apr 9, 2021

I would like this feature, too. Will try the code in the meantime. Please add my vote! Thank you.

@yoancutillas
Copy link
Collaborator

yoancutillas commented Apr 9, 2021

Hello, the code is outdated, it won't work after version 1.8.9, sorry. The bookacti_validate_booking_form hook parameters and return value have changed in 1.9, so this code needs to be adapted.
I have added your vote, thank you.

@prosie
Copy link

prosie commented Apr 9, 2021

Thanks for letting me know. Maybe I'll need to find another plugin.

@yoancutillas
Copy link
Collaborator

I have updated the code for Booking Activities 1.9 and more.

@yoancutillas yoancutillas pinned this issue May 24, 2021
@yoancutillas yoancutillas mentioned this issue Jun 18, 2021
Closed
@antoinepernaud
Copy link

Ho Great job !
Is it possible to limit to X booking(s) per activitie ?

@yoancutillas
Copy link
Collaborator

Sorry I missed your message. Thank you! Yes, you can adapt this code with your own conditions, this is only an example.

@antoinepernaud
Copy link

Sorry I missed your message. Thank you! Yes, you can adapt this code with your own conditions, this is only an example.

Ok thank you

@rayedgar
Copy link

rayedgar commented May 2, 2022

I noticed that it won't let you reschedule and throws the same limit message with this code. ($max_bookings_per_month = 1;
$max_booked_events_per_month = 1;) or am i missing something in the code to set? And could i specify an event to limit?

@yoancutillas
Copy link
Collaborator

Hello Ray, sorry, this is just an example in the hope to help. You need to customize it to get it working as desired.

@hassounet
Copy link

I also need this feature ! will try to figure out custom code in the meantime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Booking Activities For core Booking Activities plugin custom code Custom code provided as a temporary solution pending for votes Needs more votes to be validated or to increase priority
Projects
None yet
Development

No branches or pull requests

6 participants