Home › Forums › Calendar Products › Events Calendar PRO › Add event category to product category
- This topic has 8 replies, 3 voices, and was last updated 9 years, 12 months ago by
Support Droid.
-
AuthorPosts
-
April 5, 2016 at 5:39 pm #1098711
Jayson
ParticipantI 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 );
}
}April 6, 2016 at 6:30 pm #1099313Geoff B.
MemberGood 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.
April 10, 2016 at 2:10 pm #1100627Jayson
ParticipantHi 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
DanApril 11, 2016 at 6:58 pm #1101141Geoff B.
MemberGood 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.
April 12, 2016 at 2:01 pm #1101574Geoff B.
MemberGood 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.
April 14, 2016 at 3:36 pm #1102758Jayson
ParticipantHi 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 ); } } }April 14, 2016 at 3:38 pm #1102759Jayson
ParticipantYou 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' );April 14, 2016 at 9:51 pm #1102875Geoff B.
MemberHey 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.April 29, 2016 at 9:35 am #1108867Support Droid
KeymasterThis 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. -
AuthorPosts
- The topic ‘Add event category to product category’ is closed to new replies.
