Want to set category to product WooCommerce? After some trial and error I solved it the following way to set category to product WooCommerce.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Creates woocommerce product $product = array( 'post_title' => $name, 'post_content' => '', 'post_status' => 'publish', 'post_author' => $current_user->ID, 'post_type' =>'product' ); // 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.