Tribe__Tickets__Tickets_Handler::update_shared_tickets_capacity( int $meta_id, int $object_id, string $meta_key, int $event_capacity )
On the Update of a Object (Event, Page, Post.
Contents
..) we need to do some other actions:
- Object needs to have Shared Stock Enabled
- Object needs a Shared Stock level to be set
- Shared tickets have their capacity and stock updated
Parameters
- $meta_id
-
(int) (Required) MID
- $object_id
-
(int) (Required) Which Post we are dealing with
- $meta_key
-
(string) (Required) Which meta key we are fetching
- $event_capacity
-
(int) (Required) To which value the event Capacity was update to
Return
(boolean)
Source
File: src/Tribe/Tickets_Handler.php
public function update_shared_tickets_capacity( $meta_id, $object_id, $meta_key, $event_capacity ) {
// Bail on non-capacity
if ( $this->key_capacity !== $meta_key ) {
return false;
}
$event_types = Tribe__Tickets__Main::instance()->post_types();
// Bail on non event like post type
if ( ! in_array( get_post_type( $object_id ), $event_types ) ) {
return false;
}
// 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 = array();
// Get all Tickets
$tickets = $this->get_tickets_ids( $object_id );
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;
}
$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 );
return true;
}
Changelog
| Version | Description |
|---|---|
| 4.6 | Introduced. |