Create New Ticket using PHP

Home Forums Ticket Products Event Tickets Plus Create New Ticket using PHP

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #957150
    John Osmond
    Participant

    Hi, I’m developing a plugin that will 4 or 5 new tickets for an event based on preset options. Is there a php function for creating new tickets?

    #957334
    Brian
    Keymaster

    Hi,

    Thanks for using our plugins.

    It could be possible to create tickets by code, however, the ticket product needs to be connected to an event to work so that might be a challenge to get working.

    Unfortunately, I am unable to provide much support on that.

    Let me know if you have any follow up questions.

    Thanks

    #957362
    John Osmond
    Participant

    So that’s it? Can you give me some ideas where to look.

    Clearly the WooTickets form does it thru an ajax call. Isn’t there some php functions that I can pass the event_id to along with a data array and create a ticket?

    #957517
    John Osmond
    Participant

    I think I got it figured out. This seems to be working:

    class myclass {

    public function create_tickets( $event_id ) {
    // this function will create a set of tickets for any given event
    // it looks to the acf options page for default tickets

    $sku = tribe_get_start_date($event_id, true, ‘YmdHi’); //sku base on date/time
    $start_date_time = tribe_get_start_date($event_id, true, ‘m/d @ g:i’); //becomes part of the title

    if( have_rows(‘show_default_ticket_packages’, ‘option’) ) {

    while( have_rows(‘show_default_ticket_packages’, ‘option’ ) ) {

    the_row();

    $ticket_title = get_sub_field(‘default_ticket_pkg_title’, ‘option’);
    $ticket_desc = get_sub_field(‘default_ticket_pkg_desc’, ‘option’);
    $ticket_desc = strip_tags($ticket_desc); // gotta strip tags
    $ticket_price = get_sub_field(‘default_ticket_pkg_price’, ‘option’);

    $ticket = new TribeEventsTicketObject();

    $ticket->description = $ticket_desc;
    $ticket->name = “$ticket_title $start_date_time”;
    $ticket->price = $ticket_price;

    $raw_data = array();
    $raw_data[‘ticket_woo_stock’] = 80; // TODO: parse out to new options field
    $raw_data[‘ticket_woo_sku’] = $sku;

    $this->save_ticket( $event_id, $ticket, $raw_data );

    }
    }
    }

    // this part I got from you guys
    /**
    * Saves a given ticket (WooCommerce product)
    *
    * @param int $event_id
    * @param TribeEventsTicketObject $ticket
    * @param array $raw_data
    *
    * @return bool
    */
    private function save_ticket( $event_id, $ticket, $raw_data = array() ) {
    if ( empty( $ticket->ID ) ) {
    /* Create main product post */
    $args = array( ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘product’,
    ‘post_author’ => get_current_user_id(),
    ‘post_excerpt’ => $ticket->description,
    ‘post_title’ => $ticket->name );

    $ticket->ID = wp_insert_post( $args );

    update_post_meta( $ticket->ID, ‘_visibility’, ‘hidden’ );
    update_post_meta( $ticket->ID, ‘_tax_status’, ‘taxable’ );
    update_post_meta( $ticket->ID, ‘_tax_class’, ” );
    update_post_meta( $ticket->ID, ‘_purchase_note’, ” );
    update_post_meta( $ticket->ID, ‘_weight’, ” );
    update_post_meta( $ticket->ID, ‘_length’, ” );
    update_post_meta( $ticket->ID, ‘_width’, ” );
    update_post_meta( $ticket->ID, ‘_height’, ” );
    update_post_meta( $ticket->ID, ‘_downloadable’, ‘no’ );
    update_post_meta( $ticket->ID, ‘_virtual’, ‘yes’ );
    update_post_meta( $ticket->ID, ‘_sale_price_dates_from’, ” );
    update_post_meta( $ticket->ID, ‘_sale_price_dates_to’, ” );
    update_post_meta( $ticket->ID, ‘_product_attributes’, array() );
    update_post_meta( $ticket->ID, ‘_sale_price’, ” );
    update_post_meta( $ticket->ID, ‘total_sales’, 0 );

    // Relate event <—> ticket
    $event_key = ‘_tribe_wooticket_for_event’;

    add_post_meta( $ticket->ID, $event_key, $event_id );

    } else {
    $args = array( ‘ID’ => $ticket->ID,
    ‘post_excerpt’ => $ticket->description,
    ‘post_title’ => $ticket->name );

    $ticket->ID = wp_update_post( $args );
    }

    if ( ! $ticket->ID )
    return false;

    update_post_meta( $ticket->ID, ‘_regular_price’, $ticket->price );
    update_post_meta( $ticket->ID, ‘_price’, $ticket->price );

    if ( trim( $raw_data[‘ticket_woo_stock’] ) !== ” ) {
    $stock = (int) $raw_data[‘ticket_woo_stock’];
    $status = ( 0 < $stock ) ? ‘instock’ : ‘outofstock’;

    update_post_meta( $ticket->ID, ‘_stock’, $stock );
    update_post_meta( $ticket->ID, ‘_stock_status’, $status );
    update_post_meta( $ticket->ID, ‘_backorders’, ‘no’ );
    update_post_meta( $ticket->ID, ‘_manage_stock’, ‘yes’ );
    delete_transient( ‘wc_product_total_stock_’ . $ticket->ID );
    } else {
    update_post_meta( $ticket->ID, ‘_manage_stock’, ‘no’ );
    }

    if ( isset( $raw_data[‘ticket_woo_sku’] ) )
    update_post_meta( $ticket->ID, ‘_sku’, $raw_data[‘ticket_woo_sku’] );

    if ( isset( $ticket->start_date ) ) {
    update_post_meta( $ticket->ID, ‘_ticket_start_date’, $ticket->start_date );
    } else {
    delete_post_meta( $ticket->ID, ‘_ticket_start_date’ );
    }

    if ( isset( $ticket->end_date ) ) {
    update_post_meta( $ticket->ID, ‘_ticket_end_date’, $ticket->end_date );
    } else {
    delete_post_meta( $ticket->ID, ‘_ticket_end_date’ );
    }

    wp_set_object_terms( $ticket->ID, ‘Ticket’, ‘product_cat’, true );

    return true;
    }
    }// /myclass

    #957651
    Brian
    Keymaster

    I am glad to see you were able to figure it out.

    I could have provided more information, but did not get a chance before you posted again.

    On customizations we can try to answer specific questions, but for the most part it is beyond the support we can provide per our terms and conditions to help out on the entire process.

    Having said that if you run into an issue I maybe to help.

    Thanks

    #969580
    Brian
    Keymaster

    I am going to close this topic as we typically close threads if there is no activity after two weeks. Feel free to create a new thread and reference this one to save you time.

    Thanks

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Create New Ticket using PHP’ is closed to new replies.