Tribe__Tickets__Tickets::update_capacity( WP_Post|int $ticket, $data, string $save_type )
Adds or updates the capacity for a ticket.
Contents
Parameters
- $ticket
-
(WP_Post|int) (Required)
- $raw_data
-
(array) (Required)
- $save_type
-
(string) (Required)
Source
File: src/Tribe/Tickets.php
public function update_capacity( $ticket, $data, $save_type ) {
if ( empty( $data ) ) {
return;
}
// set the default capacity to that of the event, if set, or to unlimited
$default_capacity = (int) Tribe__Utils__Array::get( $data, 'event_capacity', -1 );
// Fetch capacity field, if we don't have it use default (defined above)
$data['capacity'] = trim( Tribe__Utils__Array::get( $data, 'capacity', $default_capacity ) );
// If empty we need to modify to the default
if ( '' === $data['capacity'] ) {
$data['capacity'] = $default_capacity;
}
// The only available value lower than zero is -1 which is unlimited
if ( 0 > $data['capacity'] ) {
$data['capacity'] = -1;
}
// Fetch the stock if defined, otherwise use Capacity field
$data['stock'] = trim( Tribe__Utils__Array::get( $data, 'stock', $data['capacity'] ) );
// If empty we need to modify to what every capacity was
if ( '' === $data['stock'] ) {
$data['stock'] = $data['capacity'];
}
// The only available value lower than zero is -1 which is unlimited
if ( 0 > $data['stock'] ) {
$data['stock'] = -1;
}
if ( -1 !== $data['capacity'] ) {
if ( 'update' === $save_type ) {
/** @var Tribe__Tickets__Tickets_Handler $tickets_handler */
$tickets_handler = tribe( 'tickets.handler' );
$totals = $tickets_handler->get_ticket_totals( $ticket->ID );
$data['stock'] -= $totals['pending'] + $totals['sold'];
}
update_post_meta( $ticket->ID, '_manage_stock', 'yes' );
update_post_meta( $ticket->ID, '_stock', $data['stock'] );
} else {
// unlimited stock
delete_post_meta( $ticket->ID, '_stock_status' );
update_post_meta( $ticket->ID, '_manage_stock', 'no' );
delete_post_meta( $ticket->ID, '_stock' );
delete_post_meta( $ticket->ID, Tribe__Tickets__Global_Stock::TICKET_STOCK_MODE );
delete_post_meta( $ticket->ID, Tribe__Tickets__Global_Stock::TICKET_STOCK_CAP );
}
tribe_tickets_update_capacity( $ticket, $data['capacity'] );
}
Changelog
| Version | Description |
|---|---|
| 4.7 | Introduced. |