Tribe__Tickets_Plus__Commerce__WooCommerce__Cart::commerce_get_tickets_in_cart( array $tickets = array() )
Get all tickets currently in the cart for Commerce.
Contents
Parameters
- $tickets
-
(array) (Optional) List of tickets.
Default value: array()
Return
(array) List of tickets.
Source
File: src/Tribe/Commerce/WooCommerce/Cart.php
public function commerce_get_tickets_in_cart( $tickets = [] ) {
try {
$wc = WC();
// If the cart is null, we need to bail to prevent any "Call to a member function on null" errors
if ( is_null( $wc->cart ) ) {
wc_load_cart();
// API requests need extra things available for the cart.
if ( $wc->is_rest_api_request() ) {
$wc->frontend_includes();
$cart_session = new WC_Cart_Session( $wc->cart );
$cart_session->maybe_set_cart_cookies();
}
}
$contents = $wc->cart->get_cart_contents();
} catch ( Exception $exception ) {
return $tickets;
}
if ( empty( $contents ) ) {
return $tickets;
}
/** @var \Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo */
$commerce_woo = tribe( 'tickets-plus.commerce.woo' );
$event_key = $commerce_woo->event_key;
$optout_key = $commerce_woo->attendee_optout_key;
foreach ( $contents as $item ) {
$ticket_id = $item['product_id'];
$ticket_quantity = $item['quantity'];
$optout = false;
if ( isset( $item[ $optout_key ] ) ) {
$optout = $item[ $optout_key ];
}
$post_id = (int) get_post_meta( $ticket_id, $event_key, true );
if ( empty( $post_id ) ) {
continue;
}
$optout = filter_var( $optout, FILTER_VALIDATE_BOOLEAN );
$optout = $optout ? 'yes' : 'no';
$tickets[] = [
'ticket_id' => $ticket_id,
'quantity' => $ticket_quantity,
'post_id' => $post_id,
'optout' => $optout,
'provider' => 'woo',
];
}
/**
* Allows for filtering the returned tickets for easier third-party plugin compatibility.
*
* @since 4.10.8
*
* @param array $tickets List of tickets currently in the cart.
* @param array $contents The WooCommerce cart contents.
*/
return apply_filters( 'tribe_tickets_plus_woocommerce_tickets_in_cart', $tickets, $contents );
}
Changelog
| Version | Description |
|---|---|
| 4.11.0 | Introduced. |