Tribe__Tickets__RSVP::create_attendee_for_ticket( Tribe__Tickets__Ticket_Object $ticket, array $attendee_data )
Create an attendee for a RSVP ticket.
Contents
Parameters
- $ticket
-
(Tribe__Tickets__Ticket_Object) (Required) Ticket object.
- $attendee_data
-
(array) (Required) Attendee data.
Return
(int) Attendee ID.
Source
File: src/Tribe/RSVP.php
public function create_attendee_for_ticket( $ticket, $attendee_data ) {
$rsvp_options = \Tribe__Tickets__Tickets_View::instance()->get_rsvp_options( null, false );
$required_details = [
'full_name',
'email',
];
foreach ( $required_details as $required_detail ) {
// Detail is not set.
if ( ! isset( $attendee_data[ $required_detail ] ) ) {
/* translators: %s is the attendee field name. */
throw new Exception( sprintf( __( 'Attendee field "%s" is not set.', 'event-tickets' ), $required_detail ) );
}
// Detail is empty.
if ( empty( $attendee_data[ $required_detail ] ) ) {
/* translators: %s is the attendee field name. */
throw new Exception( sprintf( __( 'Attendee field "%s" is empty.', 'event-tickets' ), $required_detail ) );
}
}
$full_name = $attendee_data['full_name'];
$email = $attendee_data['email'];
$optout = true;
$user_id = isset( $attendee_data['user_id'] ) ? (int) $attendee_data['user_id'] : 0;
$order_status = isset( $attendee_data['order_status'] ) ? $attendee_data['order_status'] : 'yes';
$order_id = ! empty( $attendee_data['order_id'] ) ? $attendee_data['order_id'] : $this->generate_order_id();
$product_id = $ticket->ID;
$order_attendee_id = isset( $attendee_data['order_attendee_id'] ) ? $attendee_data['order_attendee_id'] : null;
if ( isset( $attendee_data['optout'] ) && '' !== $attendee_data['optout'] ) {
$optout = tribe_is_truthy( $attendee_data['optout'] );
}
if ( 'going' === $order_status ) {
$order_status = 'yes';
} elseif ( 'not-going' === $order_status ) {
$order_status = 'no';
}
if ( ! isset( $rsvp_options[ $order_status ] ) ) {
$order_status = 'yes';
}
// Get the event this ticket is for.
$post_id = (int) get_post_meta( $product_id, $this->event_key, true );
if ( empty( $post_id ) ) {
throw new Exception( __( 'Unable to process your request, invalid content resource.', 'event-tickets' ) );
}
$attendee = [
'post_status' => 'publish',
'post_title' => $full_name,
'post_type' => $this->attendee_object,
'ping_status' => 'closed',
'post_author' => 0,
];
if ( $order_id ) {
$attendee['post_title'] = $order_id . ' | ' . $attendee['post_title'];
}
if ( null !== $order_attendee_id ) {
$attendee['post_title'] .= ' | ' . $order_attendee_id;
}
// Insert individual ticket purchased.
$attendee_id = wp_insert_post( $attendee );
if ( is_wp_error( $attendee_id ) ) {
throw new Exception( $attendee_id->get_error_message() );
}
// @todo This class is not setting $this->attendee_product_key.
update_post_meta( $attendee_id, self::ATTENDEE_PRODUCT_KEY, $product_id );
// @todo This class is not setting $this->attendee_event_key.
update_post_meta( $attendee_id, self::ATTENDEE_EVENT_KEY, $post_id );
update_post_meta( $attendee_id, $this->security_code, $this->generate_security_code( $attendee_id ) );
update_post_meta( $attendee_id, $this->order_key, $order_id );
// @todo This class is not setting $this->attendee_optout_key.
update_post_meta( $attendee_id, self::ATTENDEE_OPTOUT_KEY, (int) $optout );
if ( 0 === $user_id ) {
/**
* Allow enabling user lookups by Attendee Email.
*
* @since5.0.0
*
* @param boolean $lookup_user_from_email Whether to lookup the User using the Attendee Email if User ID not set.
*/
$lookup_user_from_email = apply_filters( 'tribe_tickets_rsvp_create_attendee_lookup_user_from_email', false );
if ( $lookup_user_from_email ) {
// Check if user exists.
$user = get_user_by( 'email', $email );
if ( $user ) {
$user_id = $user->ID;
}
}
}
if ( 0 < $user_id ) {
update_post_meta( $attendee_id, $this->attendee_user_id, $user_id );
}
// @todo ET should add a property for this.
update_post_meta( $attendee_id, self::ATTENDEE_RSVP_KEY, $order_status );
update_post_meta( $attendee_id, $this->full_name, $full_name );
update_post_meta( $attendee_id, $this->email, $email );
update_post_meta( $attendee_id, '_paid_price', 0 );
// Get the RSVP status `decrease_stock_by` value.
$status_stock_size = $rsvp_options[ $order_status ]['decrease_stock_by'];
if ( 0 < $status_stock_size ) {
// @todo Holy race condition batman!
// Adjust total sales.
$sales = (int) get_post_meta( $product_id, 'total_sales', true );
update_post_meta( $product_id, 'total_sales', ++ $sales );
// Adjust stock.
$stock = (int) get_post_meta( $product_id, '_stock', true ) - $status_stock_size;
update_post_meta( $product_id, '_stock', $stock );
}
/**
* RSVP specific action fired when a RSVP-driven attendee ticket for an event is generated.
* Used to assign a unique ID to the attendee.
*
* @param int $attendee_id ID of attendee ticket.
* @param int $post_id ID of event.
* @param string $order_id RSVP order ID (hash).
* @param int $product_id RSVP product ID.
*/
do_action( 'event_tickets_rsvp_attendee_created', $attendee_id, $post_id, $order_id, $product_id );
/**
* Action fired when an RSVP attendee ticket is created.
* Used to store attendee meta.
*
* @param int $attendee_id ID of the attendee post.
* @param int $post_id Event post ID.
* @param int $product_id RSVP ticket post ID.
* @param int $order_attendee_id Attendee # for order.
*/
do_action( 'event_tickets_rsvp_ticket_created', $attendee_id, $post_id, $product_id, $order_attendee_id );
if ( null === $order_attendee_id ) {
/**
* Action fired when an RSVP ticket has had attendee tickets generated for it.
*
* @param int $product_id RSVP ticket post ID.
* @param string $order_id ID (hash) of the RSVP order.
* @param int $qty Quantity ordered.
*/
do_action( 'event_tickets_rsvp_tickets_generated_for_product', $product_id, $order_id, 1 );
$this->clear_attendees_cache( $post_id );
}
return $attendee_id;
}
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |