Tribe__Tickets_Plus__Commerce__WooCommerce__Cart::commerce_update_tickets_in_cart( array $tickets, int $post_id, boolean $additive )

Update tickets in WooCommerce cart for Commerce.


Parameters

$tickets

(array) (Required) List of tickets with their ID and quantity.

$post_id

(int) (Required) Post ID for the cart.

$additive

(boolean) (Required) Whether to add or replace tickets.


Top ↑

Source

File: src/Tribe/Commerce/WooCommerce/Cart.php

	public function commerce_update_tickets_in_cart( $tickets, $post_id, $additive ) {
		$wc = WC();

		// Include files and setup session/cart.
		$wc->frontend_includes();
		$wc->initialize_session();
		$wc->initialize_cart();

		/** @var \Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo */
		$commerce_woo = tribe( 'tickets-plus.commerce.woo' );

		$optout_key = $commerce_woo->attendee_optout_key;

		/** @var Tribe__Tickets__REST__V1__Messages $messages */
		$messages = tribe( 'tickets.rest-v1.messages' );

		foreach ( $tickets as $ticket ) {
			// Skip if ticket ID not set.
			if ( empty( $ticket['ticket_id'] ) ) {
				continue;
			}

			$ticket_id       = $ticket['ticket_id'];
			$ticket_quantity = $ticket['quantity'];

			// Get the ticket object.
			$ticket_object = $commerce_woo->get_ticket( 0, $ticket_id );

			// Bail if ticket does not exist.
			if ( ! $ticket_object ) {
				$error_code = 'ticket-does-not-exist';

				throw new Tribe__REST__Exceptions__Exception( sprintf( $messages->get_message( $error_code ), $ticket_id ), $error_code, 500 );
			}

			// Get the number of available tickets.
			$available = $ticket_object->available();

			// Bail if ticket does not have enough available capacity.
			if ( ( -1 !== $available && $available < $ticket_quantity ) || ! $ticket_object->date_in_range() ) {
				$error_code = 'ticket-capacity-not-available';

				throw new Tribe__REST__Exceptions__Exception( sprintf( $messages->get_message( $error_code ), $ticket_object->name ), $error_code, 500 );
			}

			$optout = filter_var( $ticket['optout'], FILTER_VALIDATE_BOOLEAN );
			$optout = $optout ? 'yes' : 'no';

			$extra_data = [
				$optout_key => $optout,
			];

			$this->add_ticket_to_cart( $ticket_id, $ticket_quantity, $extra_data, $additive );
		}
	}

Top ↑

Changelog

Changelog
Version Description
4.11.0 Introduced.