Add event category to product category

Home Forums Calendar Products Events Calendar PRO Add event category to product category

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1098711
    Jayson
    Participant

    I have added this function trying to get categories added to the ticket products. Tickets shows up, but it is not parsing the event categories. What am I doing wrong?

    // add all tickets to a ticket category
    add_filter( ‘wootickets_after_save_ticket’, ‘tribe_add_category_to_woocommerce_tickets’ );
    function tribe_add_category_to_woocommerce_tickets( $event_id ) {
    wp_set_object_terms( $event_id, ‘Ticket’, ‘product_cat’, true );

    $terms = get_the_terms($event_id, ‘tribe_events_cat’);
    $count = count($terms);
    if ( $count > 0 ){
    foreach ( $terms as $term ) {

    wp_set_object_terms( $event_id, $term->slug, ‘product_cat’, true );

    }
    }

    #1099313
    Geoff B.
    Member

    Good evening Dan and welcome to the Events Calendar Support forum!

    Thank you for reaching out to us.

    Just to set expectations, as you might know, the scope of our support is mostly to get our wonderful customers started on the right track to customize their site and to help them in case of issues. We unfortunately do not provide complete support for customization.

    With that in mind, I took a quick look at your code and I believe the reason it is not working properly is because the array you are trying to return to the action does not match the one found in the Events Tickets Plus code (/wp-content/plugins/event-tickets-plus/src/Tribe/Commerce/WooCommerce/Main.php) at line 520.

    In other words, you have to respect or extend the structure of the action if you want to add this extra information in.

    Furthermore, perhaps there is a simpler way to achieve what you are looking for (if you could expand a bit on what your goal is) ?

    Best regards,

    Geoff B.

    #1100627
    Jayson
    Participant

    Hi Geof

    When an event ticket is created I want the related woocommerce product to be in a matching category. This code tries to do it twice:

    1. Add product to a ‘ticket’ category – working

    2. Add the events categories to the woocommerce product – not working
    For example, if the event is in the ‘workshop’ event category we want the related ticket product to be in the ‘workshop’ product category.

    Does this make sense?

    This code needs to find the event category array. I’m not sure where to find it

    Thanks
    Dan

    #1101141
    Geoff B.
    Member

    Good evening Dan,

    Thank you for taking the time to explain what you are trying to do.

    As stated before, I cannot promise anything, but I’ll still run the snippet by Dev to get a second opinion.
    In the meantime, here is some clarification on the architecture of things (which might help).

    Each the Events Calendar ticket is actually:

    • A standard WordPress post (post type = tribe_wooticket). Built-in, this post type has no relationship to the wp_terms family tables (where the categories are stored – wp_terms, wp_terms_meta, wp_term_taxonomy, wp_term_relationship). This, of course, can be extended. But you’ll need some code for it. I believe this is where you are currently running into problems.
    • A WooCommerce virtual product (ticket). Apparently, you already have this part covered 🙂

    Let me know if that helps.

    Have a great day!

    Geoff B.

     

    #1101574
    Geoff B.
    Member

    Good afternoon Dan,

    As promised, I had somebody take a look at the code and here’s what they came up with:

    1) WooTickets are submitted via Ajax. So the action ‘wootickets_after_save_ticket’, can actually run before you have selected any categories and updated the post. Instead you might be interested in the more generic action save_post, and checking if the saved post is an event and if that event has tickets. If so, update the attached tickets.

    2) The action ‘wootickets_after_save_ticket’ has three variables it passes: $ticket_id, $event_id, $raw_data. You are using ticket_id but you really should use the event_id.

    Let me know if that helps.

    Have a great day!

    Geoff B.

    #1102758
    Jayson
    Participant

    Hi Geoff

    I got this one working. It might be worth including a simliar functionality in the plugin as it makes sense to me and helps with other woo reporting.

    // add all tickets to a ticket category and then add the event category to the ticket product
    add_filter( 'wootickets_after_save_ticket', 'tribe_add_category_to_woocommerce_tickets' );
    function tribe_add_category_to_woocommerce_tickets( $ticket_id ) {
    
    wp_set_object_terms( $ticket_id , 'Ticket', 'product_cat', true );
       
    
    $event_REAL_id= get_post_meta( $ticket_id, '_tribe_wooticket_for_event', true );
    $terms = get_the_terms($event_REAL_id, 'tribe_events_cat');
    
    	$count = count($terms);
    	if ( $count > 0 ){
    		foreach ( $terms as $term ) {
    
    wp_set_object_terms( $ticket_id , $term->slug, 'product_cat', true );
    
    		}
    	}
    }
    #1102759
    Jayson
    Participant

    You are right that the generic action of “save_post” needs to be implemented as well. I’m a bit stuck on how to find all of the ticket ids associated with an event:

    
    //on event post save, add categories to all tickets 
    function updated_ticket_product_cat( $post_id ) {
    
    //get categories
    $terms = get_the_terms($post_id, 'tribe_events_cat');
    
    //Get ticket id
    $ticket_id="????????????????";
    
    //add categories to tickets
    	$count = count($terms);
    	if ( $count > 0 ){
    		foreach ( $terms as $term ) {
                     wp_set_object_terms( $ticket_id , $term->slug, 'product_cat', true );
    		}
    	}
    }
    add_action( 'save_post', 'updated_ticket_product_cat' );
    
    #1102875
    Geoff B.
    Member

    Hey Jayson,

    You are in luck! My awesome colleague Brook took a look at this and suggested you would be interested in the following function that returns an array containing exactly what you are looking for!

    Tribe__Tickets__Tickets::get_all_event_tickets( $event->ID );

    Let me know how that goes.

    Best regards,
    Geoff B.

    #1108867
    Support Droid
    Keymaster

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Add event category to product category’ is closed to new replies.