Tribe__Tickets__REST__V1__Post_Repository::get_ticket_capacity( int $ticket_id, bool $get_details = false )
Returns a ticket capacity or capacity details.
Contents
Parameters
- $ticket_id
-
(int) (Required) The ticket ID.
- $get_details
-
(bool) (Optional) Whether to get capacity details or not. Defaults to
false. If set totruereturns an array.Default value: false
Return
(array|bool|int) The ticket capacity, the details if $get_details is set to true or false on failure.
Source
File: src/Tribe/REST/V1/Post_Repository.php
public function get_ticket_capacity( $ticket_id, $get_details = false ) {
$ticket = $this->get_ticket_object( $ticket_id );
if ( $ticket instanceof WP_Error ) {
return false;
}
$capacity = $ticket->capacity();
if ( ! $get_details ) {
return $capacity;
}
/**
* Here we use the `Tribe__Tickets__Ticket_Object::stock()` method in
* place of the `Tribe__Tickets__Ticket_Object::available()` one to make
* sure we get the value that users would see on the front-end in the
* ticket form.
*/
$available = $ticket->stock();
$unlimited = - 1 === $available;
if ( $unlimited ) {
$available_percentage = 100;
} else {
$available_percentage = $capacity <= 0 || $available == 0 ? 0 : (int) floor( $available / $capacity * 100 );
}
// @todo here we need to uniform the return values to indicate unlimited and oversold!
$details = array(
'available_percentage' => $available_percentage,
'available' => (int) $ticket->stock(), // see not above about why we use this
);
if ( current_user_can( 'read_private_posts' ) ) {
$details['max'] = (int) $ticket->capacity();
$details['sold'] = (int) $ticket->qty_sold();
$details['pending'] = (int) $ticket->qty_pending();
}
return $details;
}
Changelog
| Version | Description |
|---|---|
| 4.8 | Introduced. |