cliff
12/16/2018 - 5:46 AM

Event Tickets: Set the maximum quantity allowed per ticket.

Event Tickets: Set the maximum quantity allowed per ticket.

<?php

/**
 * Event Tickets Plus (ET+): Limit to purchasing 4 WooCommerce tickets at a time for a specific event and/or ticket.
 *
 * !!! Change the maximum and Event ID to your own values!!!
 * Requires ET+ version 4.10.7 or later to also work for tickets with an unlimited stock/quantity (value of "-1").
 *
 * @link https://gist.github.com/cliffordp/61cd8374a80d1b2bc76a6650e9304a63 This snippet.
 *
 * @see  \Tribe__Tickets__Tickets_Handler::get_ticket_max_purchase() Where this filter fires.
 * @see  \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::filter_ticket_max_purchase() Hook needs to be higher than
 *                                                                                       priority 10 because of this.
 *
 * @param Tribe__Tickets__Ticket_Object $ticket    Ticket Object
 * @param WP_Post                       $event     Event post
 * @param int                           $ticket_id Ticket Raw ID
 *
 * @param int                           $available Max Purchase number
 *
 * @return int
 */
function cliff_event_tickets_max_qty( $available, $ticket, $event, $ticket_id ) {
	// TODO: Change this to what you want!
	$max_qty = 4;

	// TODO: Only affect a specific event - change to your Event ID or remove this logic
	if ( 154 !== $event->ID ) {
		return $available;
	}

	// TODO: And/Or only affect a single ticket
	if ( 1129 !== $ticket_id ) {
		return $available;
	}

	$allowed = min( $max_qty, $available );

	// Account for unlimited quantity
	if ( - 1 === $allowed ) {
		$allowed = $max_qty;
	}

	return $allowed;
}

add_filter( 'tribe_tickets_get_ticket_max_purchase', 'cliff_event_tickets_max_qty', 50, 4 );