Tribe__Tickets__Tickets_Handler::sync_shared_capacity( int $object_id, string $event_capacity )
Sync shared capacity for the given Post/Event object.
Contents
Parameters
- $object_id
-
(int) (Required) Which Post we are dealing with.
- $event_capacity
-
(string) (Required) To which value the event Capacity was update to.
Return
(bool)
Source
File: src/Tribe/Tickets_Handler.php
public function sync_shared_capacity( $object_id, $event_capacity ) {
// We don't accept any non-numeric values here.
if ( ! is_numeric( $event_capacity ) ) {
return false;
}
// Make sure we are updating the Shared Stock when we update it's capacity.
$object_stock = new Tribe__Tickets__Global_Stock( $object_id );
// Make sure that we have stock enabled (backwards compatibility).
$object_stock->enable();
$completes = [];
// Get all Tickets.
$tickets = $this->get_tickets_ids( $object_id );
// If no tickets are available and capacity is set then it should be true.
$has_shared_cap_tickets = empty( $tickets );
foreach ( $tickets as $ticket ) {
$mode = get_post_meta( $ticket, Tribe__Tickets__Global_Stock::TICKET_STOCK_MODE, true );
// Skip any tickets that are not Shared.
if (
Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE !== $mode
&& Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE !== $mode
) {
continue;
}
$has_shared_cap_tickets = true;
$capacity = tribe_tickets_get_capacity( $ticket );
// When Global Capacity is higher than local ticket one's we bail.
if (
Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $mode
) {
$capped_capacity = $capacity;
if ( $event_capacity < $capacity ) {
$capped_capacity = $event_capacity;
}
// Otherwise we update tickets required.
tribe_tickets_update_capacity( $ticket, $capped_capacity );
}
$totals = $this->get_ticket_totals( $ticket );
$completes[] = $complete = $totals['pending'] + $totals['sold'];
$stock = $event_capacity - $complete;
update_post_meta( $ticket, '_stock', $stock );
// Makes sure we mark it as in Stock for the status.
if ( 0 !== $stock ) {
update_post_meta( $ticket, '_stock_status', 'instock' );
}
}
// Setup the Stock level.
$new_object_stock = $event_capacity - array_sum( $completes );
$object_stock->set_stock_level( $new_object_stock );
if ( ! $has_shared_cap_tickets ) {
$object_stock->disable();
$object_stock->set_stock_level( 0 );
tribe_tickets_delete_capacity( $object_id );
}
return true;
}