Tribe__Tickets_Plus__Commerce__EDD__Global_Stock::adjust_stock_levels( array $quantities )

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.


Parameters

$quantities

(array) (Required)


Top ↑

Source

File: src/Tribe/Commerce/EDD/Global_Stock.php

	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 );

			if ( ! $event ) {
				continue;
			}

			$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, Tribe__Tickets__Global_Stock::TICKET_STOCK_CAP, $original_level - $amount_purchased );
				// 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;
			}
		}
	}