Tribe__Tickets__RSVP::generate_tickets_for( int $product_id, int $ticket_qty, array $attendee_details, boolean $redirect = true )
Generates a number of attendees for an RSVP ticket.
Contents
Parameters
- $product_id
-
(int) (Required) The ticket post ID.
- $ticket_qty
-
(int) (Required) The number of attendees that should be generated.
- $attendee_details
-
(array) (Required) An array containing the details for the attendees that should be generated.
- $redirect
-
(boolean) (Optional) Whether to redirect on error.
Default value: true
Return
(array|WP_Error) true if the attendees were successfully generated, false otherwise. If $redirect is set to false, upon success this method will return an array of attendee IDs generated.
Source
File: src/Tribe/RSVP.php
public function generate_tickets_for( $product_id, $ticket_qty, $attendee_details ) {
$rsvp_options = $this->tickets_view->get_rsvp_options( null, false );
$required_details = array(
'full_name',
'email',
'order_status',
'optout',
'order_id',
);
foreach ( $required_details as $required_detail ) {
if ( ! isset( $attendee_details[ $required_detail ] ) ) {
return false;
}
if ( $required_detail !== 'optout' ) {
// some details should not be empty
if ( empty( $attendee_details[ $required_detail ] ) ) {
return false;
}
}
}
$attendee_full_name = $attendee_details['full_name'];
$attendee_email = $attendee_details['email'];
$attendee_order_status = $attendee_details['order_status'];
$attendee_optout = $attendee_details['optout'];
$order_id = $attendee_details['order_id'];
$order_attendee_id = 0;
// Get the event this tickets is for
$post_id = get_post_meta( $product_id, $this->event_key, true );
if ( empty( $post_id ) ) {
return false;
}
/** @var Tribe__Tickets__Ticket_Object $ticket_type */
$ticket_type = $this->get_ticket( $post_id, $product_id );
// get the RSVP status `decrease_stock_by` value
$status_stock_size = $rsvp_options[ $attendee_order_status ]['decrease_stock_by'];
// to avoid tickets from not being created on a status stock size of 0
// let's take the status stock size into account and create a number of tickets
// at least equal to the number of tickets the user requested
$ticket_qty = $status_stock_size < 1 ? $ticket_qty : $status_stock_size * $ticket_qty;
$qty = max( $ticket_qty, 0 );
// Throw an error if Qty is bigger then Remaining
if ( $ticket_type->managing_stock() && $qty > $ticket_type->inventory() ) {
$url = add_query_arg( 'rsvp_error', 2, get_permalink( $post_id ) );
wp_redirect( esc_url_raw( $url ) );
tribe_exit();
}
/**
* RSVP specific action fired just before a RSVP-driven attendee ticket for an event is generated
*
* @param int $post_id ID of event
* @param Tribe__Tickets__Ticket_Object $ticket_type Ticket Type object for the product
* @param array $_POST Parameters coming from the RSVP Form
*/
do_action( 'tribe_tickets_rsvp_before_attendee_ticket_creation', $post_id, $ticket_type, $_POST );
// Iterate over all the amount of tickets purchased (for this product)
for ( $i = 0; $i < $qty; $i ++ ) {
$attendee = array(
'post_status' => 'publish',
'post_title' => $attendee_full_name . ' | ' . ( $i + 1 ),
'post_type' => self::ATTENDEE_OBJECT,
'ping_status' => 'closed',
);
// Insert individual ticket purchased
$attendee_id = wp_insert_post( $attendee );
if ( $status_stock_size > 0 ) {
// 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 );
}
update_post_meta( $attendee_id, self::ATTENDEE_PRODUCT_KEY, $product_id );
update_post_meta( $attendee_id, self::ATTENDEE_EVENT_KEY, $post_id );
update_post_meta( $attendee_id, self::ATTENDEE_RSVP_KEY, $attendee_order_status );
update_post_meta( $attendee_id, $this->security_code, $this->generate_security_code( $attendee_id ) );
update_post_meta( $attendee_id, $this->order_key, $order_id );
update_post_meta( $attendee_id, self::ATTENDEE_OPTOUT_KEY, (bool) $attendee_optout );
update_post_meta( $attendee_id, $this->full_name, $attendee_full_name );
update_post_meta( $attendee_id, $this->email, $attendee_email );
update_post_meta( $attendee_id, '_paid_price', 0 );
/**
* RSVP specific action fired when a RSVP-driven attendee ticket for an event is generated
*
* @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 );
/**
* Action fired when an RSVP attendee ticket is created
*
* @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 );
$this->record_attendee_user_id( $attendee_id );
$order_attendee_id++;
}
/**
* Action fired when an RSVP 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, $qty );
// After Adding the Values we Update the Transient
Tribe__Post_Transient::instance()->delete( $post_id, Tribe__Tickets__Tickets::ATTENDEES_CACHE );
return true;
}
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Return WP_Error in case of errors to show proper error messages. |
| 4.7 | Introduced. |