Last active
May 27, 2025 09:06
-
-
Save andrasguseo/703720ca04ec593fd792210bd8a287d4 to your computer and use it in GitHub Desktop.
ETP > Importing Tickets Commerce Tickets from a CSV File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Import tweak to allow importing Tickets Commerce Tickets from a CSV file | |
* | |
* Usage: Add the snippet with Code Snippets or to your functions.php file. | |
* Install and activate WooCommerce. | |
* Run the ticket import according to https://theeventscalendar.com/knowledgebase/importing-data-from-a-csv-file/ | |
* Confirm the tickets are created. | |
* Deactivate and delete WooCommerce if not needed | |
* | |
* @author: Andras Guseo | |
* | |
* @see https://theeventscalendar.com/knowledgebase/importing-data-from-a-csv-file/ | |
* @see https://theeventscalendar.com/knowledgebase/csv-file-examples-for-importing/#ticket-fields | |
* | |
* Plugins required: The Events Calendar, Event Tickets, Event Tickets Plus | |
* | |
* @since May 27, 2025 Initial version. | |
*/ | |
add_action( 'tribe_tickets_plus_after_csv_import_ticket_created', function ( $ticket_id, $record, $data, $dis ) { | |
/** | |
* Change the post type of the post with ID of $ticket_id to 'tec_tc_ticket' | |
*/ | |
// Change the post type to 'tec_tc_ticket' | |
$updated = wp_update_post( [ | |
'ID' => $ticket_id, | |
'post_type' => 'tec_tc_ticket' | |
] ); | |
// Optional error checking, bail if error. | |
if ( is_wp_error( $updated ) ) { | |
// Handle error if needed | |
error_log( 'Failed to update ticket post type: ' . $updated->get_error_message() ); | |
return; | |
} | |
/** | |
* Get the _tribe_wooticket_for_event postmeta of $ticket_id and change the postmeta key to _tec_tickets_commerce_event. | |
*/ | |
$event_id = get_post_meta( $ticket_id, '_tribe_wooticket_for_event', true ); | |
if ( ! empty( $event_id ) ) { | |
// Delete the old meta key | |
delete_post_meta( $ticket_id, '_tribe_wooticket_for_event' ); | |
// Add the new meta key with the same value | |
update_post_meta( $ticket_id, '_tec_tickets_commerce_event', $event_id ); | |
} | |
/** | |
* Get the _ticket_start_date postmeta of $ticket_id, which is YYYY-MM-DD HH:MM:SS and convert it to: | |
* - _ticket_end_date which is YYYY-MM-DD | |
* - _ticket_end_time which is HH:MM:SS | |
*/ | |
$start_date_time = get_post_meta( $ticket_id, '_ticket_start_date', true ); | |
if ( ! empty( $start_date_time ) ) { | |
// Parse the datetime string | |
$date_time = new DateTime( $start_date_time ); | |
// Extract and format date as YYYY-MM-DD | |
$date = $date_time->format( 'Y-m-d' ); | |
// Extract and format time as HH:MM:SS | |
$time = $date_time->format( 'H:i:s' ); | |
// Update the post meta with the new format | |
update_post_meta( $ticket_id, '_ticket_start_date', $date ); | |
update_post_meta( $ticket_id, '_ticket_start_time', $time ); | |
} | |
/** | |
* Get the _ticket_end_date postmeta of $ticket_id, which is YYYY-MM-DD HH:MM:SS and convert it to: | |
* - _ticket_end_date which is YYYY-MM-DD | |
* - _ticket_end_time which is HH:MM:SS | |
*/ | |
$end_date_time = get_post_meta( $ticket_id, '_ticket_end_date', true ); | |
if ( ! empty( $end_date_time ) ) { | |
// Parse the datetime string | |
$date_time = new DateTime( $end_date_time ); | |
// Extract and format date as YYYY-MM-DD | |
$date = $date_time->format( 'Y-m-d' ); | |
// Extract and format time as HH:MM:SS | |
$time = $date_time->format( 'H:i:s' ); | |
// Update the post meta with the new format | |
update_post_meta( $ticket_id, '_ticket_end_date', $date ); | |
update_post_meta( $ticket_id, '_ticket_end_time', $time ); | |
} | |
}, 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment