Tribe__Tickets__Ticket_Object::inventory()

Provides the Inventory of the Ticket which should match the Commerce Stock


Return

(int)


Top ↑

Source

File: src/Tribe/Ticket_Object.php

		public function inventory() {
			// Fetch provider
			$provider = $this->get_provider();
			$capacity = $this->capacity();

			// If we don't have the provider we fetch from inventory
			if ( is_null( $provider ) || ! method_exists( $provider, 'get_attendees_by_id' ) ) {
				return $capacity - $this->qty_sold() - $this->qty_pending();
			}

			// if we aren't tracking stock, then always assume it is in stock or capacity is unlimited
			if ( ! $this->managing_stock() || -1 === $capacity ) {
				return -1;
			}

			// Fetch the Attendees
			$attendees = $this->provider->get_attendees_by_id( $this->ID );
			$attendees_count = 0;
			$not_going_arr = tribe( 'tickets.status' )->get_statuses_by_action( 'count_not_going', 'rsvp' );

			// Loop on All the attendees, allowing for some filtering of which will be removed or not
			foreach ( $attendees as $attendee ) {
				// Prevent RSVP with Not Going Status to decrease Inventory
				if ( ! empty( $attendee['provider_slug'] ) && 'rsvp' === $attendee['provider_slug'] && in_array( $attendee[ 'order_status' ], $not_going_arr, true ) ) {
					continue;
				}

				// allow providers to decide if an attendee will count toward inventory decrease or not
				if ( ! $this->provider->attendee_decreases_inventory( $attendee ) ) {
					continue;
				}

				$attendees_count++;
			}

			// Do the math!
			$inventory[] = $capacity - $attendees_count;

			// Calculate and verify the Event Inventory
			if (
				Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE === $this->global_stock_mode()
				|| Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $this->global_stock_mode()
			) {
				$event_attendees = $this->provider->get_attendees_by_id( $this->get_event()->ID );
				$event_attendees_count = 0;

				foreach ( $event_attendees as $attendee ) {
					$attendee_ticket_stock = new Tribe__Tickets__Global_Stock( $attendee['event_id'] );
					// bypass any potential weirdness (RSVPs or such)
					if ( empty( $attendee[ 'product_id' ] ) ) {
						continue;
					}

					$attendee_ticket_stock_mode = get_post_meta( $attendee[ 'product_id' ], Tribe__Tickets__Global_Stock::TICKET_STOCK_MODE, true );

					// On all cases of indy stock we don't add
					if (
						! $attendee_ticket_stock->is_enabled()
						|| empty( $attendee_ticket_stock_mode )
						|| Tribe__Tickets__Global_Stock::OWN_STOCK_MODE === $attendee_ticket_stock_mode
					) {
						continue;
					}

					// All the others we add to the count
					$event_attendees_count++;
				}

				$inventory[] = tribe_tickets_get_capacity( $this->get_event()->ID ) - $event_attendees_count;
			}

			$inventory = min( $inventory );

			// Prevents Negative
			return max( $inventory, 0 );
		}

Top ↑

Changelog

Changelog
Version Description
4.6
4.12.3 Introduced.