eventsmanager icon

Set default WooCommerce event product category

eventsmanager | PRO | 10/02/21 04:19:03 PM UTC | 0 ⭐ | 2301 👁️ | Never ⏰ | []
PHP |

770 B

|

None

|

0 👍

/

0 👎

<?php
/*
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.
 
For installation functions see http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
*/
 
/**
 * @param bool $result
 * @param EM_Event $EM_Event
 * @return bool
 */
function em_woocommerce_default_category( $result, $EM_Event ){
    if( $result && $EM_Event->event_rsvp ) {
        // update event product
        $sku = 'EM-' . $EM_Event->event_id;
        $product_id = wc_get_product_id_by_sku($sku);
        if( !empty($product_id) ) {
            wp_set_object_terms( $product_id, 'custom tag goes here', 'product_tag' );
        }
    }
    return $result;
}
add_filter('em_event_save', 'em_woocommerce_default_category', 99999, 2);

Comments