Cart::prepare_data( array $request_data )
Prepare the data for cart processing.
Contents
Note that most of the data that is processed here is legacy, so you will see very weird and wonky naming. Make sure when you are making modifications you consider:
- Event Tickets without ET+ additional data
- Event Ticket Plus IAC
- Event Tickets Plus Attendee Registration
Parameters
- $request_data
-
(array) (Required) Request Data to be prepared.
Return
(array)
Source
File: src/Tickets/Commerce/Cart.php
public function prepare_data( $request_data ) {
/**
* Filters the Cart data before sending to the prepare method.
*
* @since 5.1.9
*
* @param array $request_data The cart data before processing.
*/
$request_data = apply_filters( 'tec_tickets_commerce_cart_pre_prepare_data', $request_data );
if ( empty( $request_data['tribe_tickets_ar_data'] ) ) {
return [];
}
/** @var \Tribe__Tickets__Tickets_Handler $handler */
$handler = tribe( 'tickets.handler' );
$raw_data = $request_data['tribe_tickets_ar_data'];
// Attempt to JSON decode data if needed.
if ( ! is_array( $raw_data ) ) {
$raw_data = stripslashes( $raw_data );
$raw_data = json_decode( $raw_data, true );
}
$raw_data = array_merge( $request_data, $raw_data );
$data = [];
$data['post_id'] = absint( Arr::get( $raw_data, 'tribe_tickets_post_id' ) );
$data['provider'] = sanitize_text_field( Arr::get( $raw_data, 'tribe_tickets_provider', Module::class ) );
$data['tickets'] = Arr::get( $raw_data, 'tribe_tickets_tickets' );
$data['meta'] = Arr::get( $raw_data, 'tribe_tickets_meta', [] );
$tickets_meta = Arr::get( $raw_data, 'tribe_tickets', [] );
$default_ticket = [
'ticket_id' => 0,
'quantity' => 0,
'optout' => false,
'iac' => 'none',
'extra' => [],
];
/**
* @todo Determine if this should be moved into the Ticket Controller.
*/
$data['tickets'] = array_map( static function ( $ticket ) use ( $default_ticket, $handler, $tickets_meta ) {
if ( empty( $ticket['quantity'] ) ) {
return false;
}
$ticket = array_merge( $default_ticket, $ticket );
$ticket['quantity'] = (int) $ticket['quantity'];
if ( $ticket['quantity'] < 0 ) {
return false;
}
if ( ! empty( $tickets_meta[ $ticket['ticket_id'] ]['attendees'] ) ) {
$ticket['extra']['attendees'] = $tickets_meta[ $ticket['ticket_id'] ]['attendees'];
}
$ticket['extra']['optout'] = tribe_is_truthy( $ticket['optout'] );
unset( $ticket['optout'] );
$ticket['extra']['iac'] = sanitize_text_field( $ticket['iac'] );
unset( $ticket['iac'] );
$ticket['obj'] = \Tribe__Tickets__Tickets::load_ticket_object( $ticket['ticket_id'] );
if ( ! $handler->is_ticket_readable( $ticket['ticket_id'] ) ) {
return false;
}
return $ticket;
}, $data['tickets'] );
// Remove empty items.
$data['tickets'] = array_filter( $data['tickets'] );
/**
* Filters the Meta on the Data before processing.
*
* @since 5.1.9
*
* @param array $meta Meta information on the cart.
* @param array $data Data used for the cart.w
*/
$data['meta'] = apply_filters( 'tec_tickets_commerce_cart_prepare_data_meta', $data['meta'], $data );
/**
* Filters the Cart data before sending to to the Cart Repository.
*
* @since 5.1.9
*
* @param array $data The cart data after processing.
*/
return apply_filters( 'tec_tickets_commerce_cart_prepare_data', $this->get_repository()->prepare_data( $data ) );
}
Changelog
| Version | Description |
|---|---|
| 5.1.9 | Introduced. |