Tickets::fetch_data()

Returns the data about the event tickets, if any.


Return

(array) An array of objects containing the post thumbnail data.


Top ↑

Source

File: src/Tribe/Events/Views/V2/Models/Tickets.php

	public function fetch_data() {
		if ( ! $this->exist() ) {
			return [];
		}

		if ( null !== $this->data ) {
			return $this->data;
		}

		$num_ticket_types_available = 0;
		foreach( $this->all_tickets as $ticket ) {
			if ( ! tribe_events_ticket_is_on_sale( $ticket ) ) {
				continue;
			}

			$num_ticket_types_available++;
		}

		if ( ! $num_ticket_types_available ) {
			return [];
		}

		// Get an array for ticket and rsvp counts.
		$types = \Tribe__Tickets__Tickets::get_ticket_counts( $this->post_id );

		// If no rsvp or tickets return.
		if ( ! $types ) {
			return [];
		}

		$html        = [];
		$parts       = [];
		$stock_html  = '';
		$link_label  = '';
		$link_anchor = '';

		// If we have tickets or RSVP, but everything is Sold Out then display the Sold Out message.
		foreach ( $types as $type => $data ) {

			if ( ! $data['count'] ) {
				continue;
			}

			if ( ! $data['available'] ) {
				$parts[ $type . '_stock' ] = esc_html_x( 'Sold out', 'list view stock sold out', 'event-tickets' );

				// Only re-apply if we don't have a stock yet
				if ( empty( $html['stock'] ) ) {
					$html['stock'] = $parts[ $type . '_stock' ];
				}
			} else {
				$stock = $data['stock'];
				if ( $data['unlimited'] || ! $data['stock'] ) {
					// If unlimited tickets, tickets with no stock and rsvp, or no tickets and rsvp unlimited - hide the remaining count.
					$stock = false;
				}

				if ( $stock ) {
					/** @var Tribe__Settings_Manager $settings_manager */
					$settings_manager = tribe( 'settings.manager' );

					$threshold = $settings_manager::get_option( 'ticket-display-tickets-left-threshold', 0 );

					/**
					 * Overwrites the threshold to display "# tickets left".
					 *
					 * @param int   $threshold Stock threshold to trigger display of "# tickets left"
					 * @param array $data      Ticket data.
					 * @param int   $event_id  Event ID.
					 *
					 * @since 4.10.1
					 */
					$threshold = absint( apply_filters( 'tribe_display_tickets_left_threshold', $threshold, $data, $this->post_id  ) );

					if ( ! $threshold || $stock <= $threshold ) {

						$number = number_format_i18n( $stock );

						if ( 'rsvp' === $type ) {
							$text = _n( '%s spot left', '%s spots left', $stock, 'event-tickets' );
						} else {
							$text = _n( '%s ticket left', '%s tickets left', $stock, 'event-tickets' );
						}

						$stock_html = esc_html( sprintf( $text, $number ) );
					}
				}

				$parts[ $type . '_stock' ] = $html['stock'] = $stock_html;

				if ( 'rsvp' === $type ) {
					$link_label  = esc_html( sprintf( _x( '%s Now', 'list view rsvp now ticket button', 'event-tickets' ), tribe_get_rsvp_label_singular( 'list_view_rsvp_now_button' ) ) );
					$link_anchor = '#rsvp-now';
				} else {
					$link_label  = esc_html( sprintf( _x( 'Get %s', 'list view buy now ticket button', 'event-tickets' ), tribe_get_ticket_label_plural( 'list_view_buy_now_button' ) ) );
					$link_anchor = '#tribe-tickets';
				}
			}
		}

		$this->data['link'] = (object) [
			'anchor' => get_permalink( $this->post_id ) . $link_anchor,
			'label'  => $link_label,
		];

		$this->data['stock'] = (object) [
			'available' => $stock_html,
		];

		return $this->data;
	}

Top ↑

Changelog

Changelog
Version Description
5.6.3 Add support for the updated anchor link from new ticket templates.
4.10.9 Introduced.