Tribe__Tickets_Plus__Commerce__EDD__Cart::commerce_update_tickets_in_cart( array $tickets, int $post_id, boolean $additive )
Update tickets in EDD cart for Commerce.
Contents
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.
Source
File: src/Tribe/Commerce/EDD/Cart.php
public function commerce_update_tickets_in_cart( $tickets, $post_id, $additive ) {
/** @var \Tribe__Tickets_Plus__Commerce__EDD__Main $commerce_edd */
$commerce_edd = tribe( 'tickets-plus.commerce.edd' );
$optout_key = $commerce_edd->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_edd->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 );
}
}
Changelog
| Version | Description |
|---|---|
| 4.11.0 | Introduced. |