Last active
March 31, 2020 20:54
-
-
Save rafsuntaskin/ce0d2b5cbd35379a83ad020d630f6031 to your computer and use it in GitHub Desktop.
Event Tickets Category Sync Tag Sync with WooCommerce product category and tag
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 | |
/** | |
* Description: Sync Event Tickets tag and category with WooCommerce product category and tag - depends on the slug | |
* Usage: Copy the snippet into your child theme's functions.php file. | |
* Change delimiter as per your needs. | |
* Make sure that the slug is same for both tag or both category to sync properly | |
* | |
* Plugins: Event Tickets | |
* Author: Rafsun Chowdhury | |
* Last updated: March 31, 2020 | |
* | |
* @link https://gist.github.com/rafsuntaskin/ce0d2b5cbd35379a83ad020d630f6031 | |
*/ | |
add_action( 'event_tickets_after_save_ticket', 'rt_sync_et_ticket_category_with_woo', 11, 4 ); | |
function rt_sync_et_ticket_category_with_woo( $post_id, $ticket, $raw_data, $class_name ) { | |
//hardcoded for WooCommerce only | |
if ( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' != $class_name ) { | |
return; | |
} | |
if ( ! class_exists( 'Tribe__Events__Main' ) ) { | |
return; | |
} | |
//get Event categories | |
$tribe_ecp = Tribe__Events__Main::instance(); | |
$terms = get_the_terms( $post_id, $tribe_ecp->get_event_taxonomy() ); | |
$term_slugs = wp_list_pluck( $terms, 'slug' ); | |
//get event tags | |
$event_tags = wp_get_post_tags( $post_id, 'post_tag' ); | |
$tag_slugs = wp_list_pluck( $event_tags, 'slug' ); | |
//bail out if not a valid ticket | |
if ( empty( $ticket ) || ! isset( $ticket->ID ) ) { | |
return; | |
} | |
//process if event has categories | |
if ( ! empty( $term_slugs ) ) { | |
foreach ( $term_slugs as $cat ) { | |
wp_add_object_terms( $ticket->ID, $cat, 'product_cat' ); | |
} | |
} | |
//process if event has tags | |
if ( ! empty( $tag_slugs ) ) { | |
wp_add_post_tags( $ticket->ID, $tag_slugs ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment