Last active
January 19, 2020 21:47
-
-
Save cliffordp/009f0b5f9cda516368545957eaf8543e to your computer and use it in GitHub Desktop.
Event Tickets (ET): Limit each logged-in user to a maximum purchase quantity per event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Event Tickets (ET): Limit each logged-in user to a maximum purchase quantity per event. | |
* | |
* !!! Change the maximum to your own value!!! | |
* Requires ET version 4.11.1 or later, since that's when front-end ticket inputs fully supported this filter. | |
* | |
* @link https://gist.github.com/cliffordp/009f0b5f9cda516368545957eaf8543e This snippet. | |
* @link https://docs.theeventscalendar.com/reference/hooks/tribe_tickets_get_ticket_max_purchase/ This filter. | |
* @link https://tribe.uservoice.com/forums/195723-feature-ideas/suggestions/18616273-limit-rsvp-or-woocommerce-ticket-registration-to-o Feature request. | |
* | |
* @see \Tribe__Tickets__Tickets_Handler::get_ticket_max_purchase() Where this filter fires. | |
* | |
* @param int $available Max purchase quantity. | |
* @param Tribe__Tickets__Ticket_Object $ticket Ticket object. | |
* @param WP_Post $event Event post. | |
* @param int $ticket_id Raw ticket ID. | |
* | |
* @return int | |
*/ | |
function cliff_event_tickets_max_qty_per_event_per_user( $available, $ticket, $event, $ticket_id ) { | |
// TODO: Change this to what you want! | |
$max_qty = 1; | |
// You should require being logged in to purchase: wp-admin > Events > Settings > Tickets > "Login Requirements" | |
$user_id = get_current_user_id(); | |
if ( empty( $user_id ) ) { | |
return $available; | |
} | |
$attendees = tribe_attendees(); | |
// Enable found() calculations. | |
$attendees->set_found_rows( true ); | |
// Do the filtering. | |
$args = [ | |
'event' => $event->ID, | |
'user' => $user_id, | |
]; | |
$attendees->by_args( $args ); | |
// Account for unlimited quantity | |
if ( -1 === $available ) { | |
$available = $max_qty; | |
} | |
// Do the overriding of the max allowed | |
$max_qty = $max_qty - $attendees->found(); | |
$max_qty = min( $max_qty, $available ); | |
// Don't allow displaying a negative quantity available | |
if ( 0 > $max_qty ) { | |
$max_qty = 0; | |
} | |
return $max_qty; | |
} | |
add_filter( 'tribe_tickets_get_ticket_max_purchase', 'cliff_event_tickets_max_qty_per_event_per_user', 50, 4 ); |
It worked, thank you!
Excellent! 💯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@onisunset
Try commenting out
'event' => $event->ID,
from the$args
and if0 !== $attendees->found()
, then set the$max_qty = 0
If that doesn't work for you, please report back what did work for you, to help others. If you have follow-up questions, I cannot provide support here so please request your customization at https://support.theeventscalendar.com/