Want to set category to product WooCommerce? After some trial and error I solved it the following way to set category to product WooCommerce.
// Creates woocommerce product
$product = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
// Insert the post into the database
$product_ID = wp_insert_post($product);
// Gets term object from Tree in the database.
$term = get_term_by('name', 'Tree', 'product_cat');
wp_set_object_terms($product_ID, $term->term_id, 'product_cat');
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.