Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save austinpray/88c55f2ee10dc756deef336234b88709 to your computer and use it in GitHub Desktop.
Save austinpray/88c55f2ee10dc756deef336234b88709 to your computer and use it in GitHub Desktop.
Automatically apply "tribe_events_cat" taxonomy terms to WooCommerce tickets when tickets are saved or updated.
<?php
// Apply product categories to newly created or saved tickets that match the The Events Calendar categories of the associated event
add_action('event_tickets_after_save_ticket', function ($event_id, $ticket, $raw_data, $classname) {
if (empty($ticket) || !isset($ticket->ID)) {
return;
}
// Apply the "event-tickets" category to add tickets so that all tickets have at least this category to start
wp_add_object_terms($ticket->ID, 'event-tickets', 'product_cat');
// Get all the other "tribe_events_cat" taxonomy terms on the event
$terms = get_the_terms($event_id, 'tribe_events_cat');
// Initialize our array of new terms to be inserted into the "product_cat" taxonomy
$product_terms = [];
// Loop through all the "tribe_events_cat" taxonomy terms on the event and create them as product_cat categories if necessary
foreach ($terms as $term) {
// Insert the matching term into the "product_cat" taxonomy
wp_insert_term($term->name, 'product_cat', [
'slug' => 'event-tickets-' . $term->slug
]);
// Put the new term in our array so we can apply them all to the ticket
$product_terms[] = 'event-tickets-' . $term->slug;
}
// Add all the new "product_cat" taxonomy terms to the ticket
wp_add_object_terms($ticket->ID, $product_terms, 'product_cat');
}, 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment