Tribe__Tickets__Ticket_Object::stock( int|null $value = null )

Method to manage the protected stock property of the Object Prevents setting stock lower then zero but may return -1.

Returns the current ticket stock level: either an integer or an empty string (TribeTicketsTicket_Object::UNLIMITED_STOCK) if stock is unlimited.


Parameters

$value

(int|null) (Optional) This will overwrite the old value

Default value: null


Top ↑

Return

(int|string)


Top ↑

Source

File: src/Tribe/Ticket_Object.php

		public function stock( $value = null ) {
			if ( null === $value ) {
				$value = null === $this->stock
					? (int) get_post_meta( $this->ID, '_stock', true )
					: $this->stock;
			}

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

			// If the Value was passed as numeric value overwrite
			if ( is_numeric( $value ) || $value === self::UNLIMITED_STOCK ) {
				$this->stock = $value;
			}

			// if stock is negative, force it to 0
			$this->stock = 0 >= $this->stock ? 0 : $this->stock;

			$stock[] = $this->stock;

			if (
				(
					Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE === $this->global_stock_mode()
					|| Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $this->global_stock_mode()
				)
				&& isset( $this->get_event()->ID )
			) {
				$stock[] = (int) get_post_meta( $this->get_event()->ID, Tribe__Tickets__Global_Stock::GLOBAL_STOCK_LEVEL, true );
			}

			// return the new Stock
			return min( $stock );
		}