Tribe__Tickets__Tickets::get_ticket_counts( int $post_id )
Get RSVP and Ticket counts for an event if tickets are currently available.
Contents
Parameters
- $post_id
-
(int) (Required) ID of parent "event" post
Return
(array)
Source
File: src/Tribe/Tickets.php
public static function get_ticket_counts( $post_id ) {
// if no post id return empty array
if ( empty( $post_id ) ) {
return [];
}
$tickets = self::get_all_event_tickets( $post_id );
// if no tickets or rsvp return empty array
if ( ! $tickets ) {
return [];
}
/**
* This order is important so that tickets overwrite RSVP on
* the Buy Now Button on the front-end
*/
$types['rsvp'] = array(
'count' => 0,
'stock' => 0,
'unlimited' => 0,
'available' => 0,
);
$types['tickets'] = array(
'count' => 0, // count of ticket types currently for sale
'stock' => 0, // current stock of tickets available for sale
'global' => 0, // global stock ticket
'unlimited' => 0, // unlimited stock tickets
'available' => 0, // are tickets available for sale right now
);
foreach ( $tickets as $ticket ) {
// If a ticket is not current for sale do not count it
if ( ! tribe_events_ticket_is_on_sale( $ticket ) ) {
continue;
}
// if ticket and not rsvp add to ticket array
if ( 'Tribe__Tickets__RSVP' !== $ticket->provider_class ) {
$types['tickets']['count'] ++;
$global_stock_mode = $ticket->global_stock_mode();
if ( $global_stock_mode === Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE && 0 === $types['tickets']['global'] ) {
$types['tickets']['global'] ++;
} elseif ( $global_stock_mode === Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE && 1 === $types['tickets']['global'] ) {
continue;
}
if ( Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE === $global_stock_mode ) {
continue;
}
$stock_level = Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $global_stock_mode ? $ticket->global_stock_cap : $ticket->available();
// whether the stock level is negative because it represents unlimited stock (`-1`)
// or because it's oversold we normalize to `0` for the sake of displaying
$stock_level = max( 0, (int) $stock_level );
$types['tickets']['stock'] += $stock_level;
if ( 0 !== $types['tickets']['stock'] ) {
$types['tickets']['available'] ++;
}
if ( ! $ticket->manage_stock() || -1 === $ticket->capacity ) {
$types['tickets']['unlimited'] ++;
$types['tickets']['available'] ++;
}
} else {
$types['rsvp']['count'] ++;
$types['rsvp']['stock'] += $ticket->stock;
if ( 0 !== $types['rsvp']['stock'] ) {
$types['rsvp']['available'] ++;
}
if ( ! $ticket->manage_stock() ) {
$types['rsvp']['unlimited'] ++;
$types['rsvp']['available'] ++;
}
}
}
$global_stock = new Tribe__Tickets__Global_Stock( $post_id );
$global_stock = $global_stock->is_enabled() ? $global_stock->get_stock_level() : 0;
$types['tickets']['available'] += $global_stock;
// If there's at least one ticket with shared capacity
if ( ! self::tickets_own_stock( $post_id ) ) {
$types['tickets']['stock'] += $global_stock;
}
return $types;
}