2014-11-30 9 views
8

è possibile aggiungere una categoria a un post di woocommerce?Come impostare la categoria sul prodotto woocommerce

sto creando i miei prodotti come segue:

// 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); 

ho una categoria chiamata "Albero" dove devo aggiungere il prodotto al. Ho provato quanto segue ma senza successo. C'è un modo speciale per aggiungere una categoria?

wp_set_object_terms($productID, array('Tree'), 'product_cat'); 

risposta

13

Dopo alcuni tentativi ed errori, risolto nel modo seguente:

// 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'); 

riferimento per ulteriori informazioni: