Update product category from event tickets

I am using The Events Category and Woocommerce for the ticketing and payments.

I successfully added a code to take the featured image from the events to automatically add it to the tickets (woocommerce products) with this code:

add_action('save_post_tribe_events', function($post_id){

if(wp_is_post_revision($post_id)) return; //Bail if this is a revision

$tickets_provider = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance();

$tickets = $tickets_provider->get_tickets_ids ( $post_id );

foreach($tickets as $ticket_id) set_post_thumbnail($ticket_id, get_post_thumbnail_id($post_id));

return;

});

Now I am trying to do the same with categories, with no luck, using this:

add_action('save_post_tribe_events', function($post_id){

if(wp_is_post_revision($post_id)) return; //Bail if this is a revision

$tickets_provider = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance();

$tickets = $tickets_provider->get_tickets_ids ( $post_id );

foreach($tickets as $ticket_id) wp_set_post_categories($ticket_id, wp_get_post_categories($post_id));

return;

});

Does anyone have any thoughts?

  • Nahid
    • Tech Support

    Hey there Nathan !

    Hope you are having a great day!

    I took a closer look at the code and it looks correct from the general point of view. However, in order to find out why it is not working, we’ll have to troubleshoot the code and test it out further. Since we do not, unfortunately, have a similar setup to test this out, would you be able to share temporary credentials to your WordPress Dashboard and file system so that we can take a closer look at the code and troubleshoot it? If so, could you also let us know where is the code currently added?

    You can send the credentials privately through our secure contact form: https://premium.wpmudev.org/contact/#i-have-a-different-question

    Send in:

    Subject: “Attn: Nahid Mohit

    – WordPress Admin Username

    – WordPress Admin Password

    – Login URL

    – FTP/cPanel credentials (host/username/password)

    – Link back to this thread for reference

    – Any other relevant URLs

    However, please note that, since this is a custom code query involving third-party plugins, we cannot guarantee a resolution from our end. We’ll try our best but if in case it turns out to be out of our scope of support, it might be considered as custom development.

    Hope this helps. We’ll be looking forward to hearing back from you. Thanks!

    Kind regards,

    Nahid

  • Nahid
    • Tech Support

    Hey there Nathan !

    Hope you are doing well today!

    Thank you for sending in the credentials. For updating the ticket category, could you please possibly give the following code a try?

    add_action( 'save_post_tribe_events', function( $post_id ) {

    //Bail if this is a revision
    if ( wp_is_post_revision( $post_id ) ) {
    return;
    }

    $event_category = get_the_terms( $post_id, 'tribe_events_cat' );
    $event_category_slug = $event_category[0]->slug;

    $ticket_cat_obj = get_term_by( 'slug', $event_category_slug, 'product_cat' );
    $ticket_cat_id = $ticket_cat_obj->term_id;
    $arr_ticket_cat_id = array( $ticket_cat_id );

    $tickets_provider = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance();
    $tickets = $tickets_provider->get_tickets_ids( $post_id );

    foreach ( $tickets as $ticket_id ) {
    wp_set_post_terms( $ticket_id, $arr_ticket_cat_id, 'product_cat', true );
    }

    return;
    } );

    Hope this helps. Please let us know if this works for you. Thanks!

    Kind regards,

    Nahid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.