Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active January 19, 2020 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/009f0b5f9cda516368545957eaf8543e to your computer and use it in GitHub Desktop.
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.
<?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 );
@cliffordp
Copy link
Author

@onisunset
Try commenting out 'event' => $event->ID, from the $args and if 0 !== $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/

@onisunset
Copy link

It worked, thank you!

@cliffordp
Copy link
Author

Excellent! 💯

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