/** * Save the current global stock properties for this event. * * @param int $post_id */ public function save_global_stock($post_id) { if (!(isset($_POST['tribe-tickets-post-settings']) && wp_verify_nonce($_POST['tribe-tickets-post-settings'], 'tribe-tickets-meta-box'))) { return; } // Bail on autosaves/bulk updates if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) { return; } $enable = !empty($_POST['tribe-tickets-enable-global-stock']); $stock = (int) @$_POST['tribe-tickets-global-stock']; $post_global_stock = new Tribe__Tickets__Global_Stock($post_id); $post_global_stock->enable($enable); $post_global_stock->set_stock_level($stock); }
/** * Adjusts global stock levels for any of the products that were just purchased. * * Expects to fire during the 'event_tickets_edd_tickets_purchased_inventory_recorded' action. * * @param array $quantities */ public function adjust_stock_levels(array $quantities) { foreach ($quantities as $ticket_id => $amount_purchased) { $ticket_id = absint($ticket_id); $event = tribe_events_get_ticket_event($ticket_id); $global_stock = new Tribe__Tickets__Global_Stock($event->ID); // We're only interested if the ticket utilizes global stock if (!$global_stock->is_enabled()) { continue; } // Try to load the actual ticket object $tickets = $this->get_event_tickets($event->ID); // Move on if we couldn't obtain the ticket object if (empty($tickets[$ticket_id])) { continue; } switch ($tickets[$ticket_id]->global_stock_mode()) { // Reduce the cap in line with the number of capped tickets that were purchased, if any case Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE: $original_level = $tickets[$ticket_id]->global_stock_cap(); update_post_meta($ticket_id, '_global_stock_cap', $original_level - $amount_purchased); // Fall-through is deliberate - capped sales still draw from the global inventory pool // Fall-through is deliberate - capped sales still draw from the global inventory pool case Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE: $original_level = $global_stock->get_stock_level(); $global_stock->set_stock_level($original_level - $amount_purchased); break; } } }
/** * When WooCommerce reduces stock levels during order processing we need to look * for global stock tickets and ensure we "equalize" the total stock levels/sales * caps as appropriate. * * @param WC_Order $order */ public function stock_equalize(WC_Order $order) { $woo_tickets = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance(); $total_ordered = array(); $capped_tickets = array(); // Get the total quantity of global stock ordered per event foreach ($order->get_items() as $item) { $product = $order->get_product_from_item($item); $event = $woo_tickets->get_event_for_ticket($product->id); $global_stock = new Tribe__Tickets__Global_Stock($event->ID); // Skip non-tickets or tickets that do not utilize global stock if (!$event || !$global_stock->is_enabled() || !$product->managing_stock()) { continue; } $ticket = $woo_tickets->get_ticket($event->ID, $product->id); switch ($ticket->global_stock_mode()) { case Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE: $capped_tickets[$product->id] = (int) $item['qty']; // Deliberate fallthrough - $total_ordered should accumulate capped *and* global quantities // Deliberate fallthrough - $total_ordered should accumulate capped *and* global quantities case Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE: $total_ordered[$event->ID] += (int) $item['qty']; } } // For each ticket product that utilizes global stock, adjust the product inventory foreach ($total_ordered as $event_id => $quantity_ordered) { $global_stock = new Tribe__Tickets__Global_Stock($event_id); $level = $global_stock->get_stock_level(); $new_level = $level - $quantity_ordered; $global_stock->set_stock_level($new_level); $this->stock_update_global_tickets($event_id, $new_level); } // Now adjust sale caps foreach ($capped_tickets as $ticket_id => $reduce_by) { $event = $woo_tickets->get_event_for_ticket($ticket_id); $ticket = $woo_tickets->get_ticket($event->ID, $ticket_id); $current = $ticket->global_stock_cap(); $new_cap = $current - $reduce_by; $ticket->global_stock_cap($new_cap); update_post_meta($ticket_id, '_global_stock_cap', $new_cap); } }