Advertisement
eventsmanager

Set default WooCommerce event product category

Oct 2nd, 2021
1,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?php
  2. /*
  3. This function will set a default category to an event product when it is saved. Note that this sets the category each time the event is saved.
  4.  
  5. For installation functions see http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  6. */
  7.  
  8. /**
  9.  * @param bool $result
  10.  * @param EM_Event $EM_Event
  11.  * @return bool
  12.  */
  13. function em_woocommerce_default_category( $result, $EM_Event ){
  14.     if( $result && $EM_Event->event_rsvp ) {
  15.         // update event product
  16.         $sku = 'EM-' . $EM_Event->event_id;
  17.         $product_id = wc_get_product_id_by_sku($sku);
  18.         if( !empty($product_id) ) {
  19.             wp_set_object_terms( $product_id, 'custom tag goes here', 'product_tag' );
  20.         }
  21.     }
  22.     return $result;
  23. }
  24. add_filter('em_event_save', 'em_woocommerce_default_category', 99999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement