Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active January 12, 2024 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/46004be80364e84f7d2e813daece2add to your computer and use it in GitHub Desktop.
Save andrasguseo/46004be80364e84f7d2e813daece2add to your computer and use it in GitHub Desktop.
ET+ > Add custom columns to Tickets CSV import
<?php
/**
* Add custom columns to Tickets CSV import.
* This example adds a column for product category (WooCommerce) and ticket fieldset.
* The values in the CSV file need to be prefixed. Please read in-line comments for instructions.
*
* Plugins required: Event Tickets Plus
* Author: Andras Guseo
* Created: December 12, 2023
*/
add_filter( 'tribe_event_import_product_column_names', 'etp_add_column_names' );
function etp_add_column_names( $column_names ) {
$column_names['ticket_category'] = "Ticket Category";
$column_names['ticket_fieldset'] = "Fieldset ID";
return $column_names;
}
add_action( 'tribe_tickets_plus_after_csv_import_ticket_created', 'etp_csv_import_add_custom_columns', 10, 4 );
function etp_csv_import_add_custom_columns( $ticket_id, $record, $data, $dis ) {
/**
* Categories in the CSV file have to start with the prefix.
* E.g. for "My category" it would be "cat_My category"
*/
$prefix_cat = "cat_";
/**
* Fieldset post IDs in the CSV file have to start with the prefix.
* E.g. for 152 it would be tfs_152
*/
$prefix_fieldset = "tfs_";
foreach ( $record as $key => $item ) {
// Check if value starts with the category prefix.
if ( substr( $item, 0, strlen( $prefix_cat ) ) === $prefix_cat ) {
// Remove the prefix
$category = trim( str_replace( $prefix_cat, '', $item ) );
// If there's still something then add category.
if ( strlen( $item ) > 0 ) {
wp_add_object_terms( $ticket_id, $category, 'product_cat' );
}
}
// Check if value starts with the fields prefix.
if ( substr( $item, 0, strlen( $prefix_fieldset ) ) === $prefix_fieldset ) {
// Remove the prefix
$fieldset_id = (int) trim( str_replace( $prefix_fieldset, '', $item ) );
// Skip, if it's not a fieldset.
if ( get_post_type( $fieldset_id ) !== 'ticket-meta-fieldset' ) {
continue;
}
// Get the field set.
$fieldset = get_post_meta( $fieldset_id, '_tribe_tickets_meta_template', true );
// If there is a field set, then update postmeta.
if ( ! empty( $fieldset ) ) {
update_post_meta( $ticket_id, '_tribe_tickets_meta', $fieldset );
update_post_meta( $ticket_id, '_tribe_tickets_meta_enabled', 'yes' );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment