Skip to content

Instantly share code, notes, and snippets.

@cliffordp

cliffordp/functions.php

Last active Jan 19, 2020
Embed
What would you like to do?
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 );
@onisunset

This comment has been minimized.

Copy link

@onisunset onisunset commented Jan 19, 2020

Thank you for this code, this is very helpful, but if you could help me I need another little customization.
I need to extend this rule for all events, not one at time. A logged-in user can register once for all events.
Can you help me? Thank you.

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Jan 19, 2020

@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

This comment has been minimized.

Copy link

@onisunset onisunset commented Jan 19, 2020

It worked, thank you!

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Jan 19, 2020

Excellent! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.