2014-11-30 4 views
8

ist es möglich, eine Kategorie zu einem woocommerce Beitrag hinzuzufügen?Wie man Kategorie zum Produkt woocommerce einstellt

ich meine Produkte erschaffe wie folgt:

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

Ich habe eine Kategorie mit dem Namen „Baum“, wo ich das obige Produkt hinzuzufügen haben. Ich habe folgendes versucht, aber ohne Erfolg. Gibt es eine spezielle Möglichkeit eine Kategorie hinzuzufügen?

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

Antwort

13

Nach einiger Versuch und Irrtum, löste es die folgende Art und Weise:

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

Referenz für weitere Informationen: