tribe_events_count_available_tickets( null $event = null )

Counts the total number of tickets still available for sale for a specific event.


Parameters

$event

(null) (Optional)

Default value: null


Top ↑

Return

(int) 0 if no tickets available, -1 if Unlimited, else integer value.


Top ↑

Source

File: src/template-tags/tickets.php

	function tribe_events_count_available_tickets( $event = null ) {

		$count = 0;

		if ( null === ( $event = tribe_tickets_parent_post( $event ) ) ) {
			return 0;
		}

		foreach ( Tribe__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {

			$global_stock_mode = $ticket->global_stock_mode();

			if ( $global_stock_mode === Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE ) {
				continue;
			}

			$stock_level = $global_stock_mode === Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE ? $ticket->global_stock_cap : $ticket->stock;

			// If we find an unlimited ticket, just return unlimited (-1) so we don't use -1 or an empty string as a numeric stock and try to do math with it
			if ( Tribe__Tickets__Ticket_Object::UNLIMITED_STOCK === $stock_level || -1 === (int) $stock_level ) {
				return -1;
			}

			$count += (int) $stock_level; // Explicit cast as a failsafe in case a string slips through
		}

		$global_stock = new Tribe__Tickets__Global_Stock( $event->ID );
		$global_stock = $global_stock->is_enabled() ? $global_stock->get_stock_level() : 0;
		$count += $global_stock;

		return $count;
	}