您好我一直在寻找一段时间,但我无法让它工作,我无法按名称获取类别,而且我也无法以编程方式将其设为产品
$product = new WC_Product(4);
write_log($product->get_title());
// Gets term object from Tree in the database.
$category = get_term_by( 'name', 'bolsas', 'product_cat' );
echo "Cat: ".$category->name;
wp_set_object_terms($product->id, $cat->term_id, 'product_cat');
我在functions.php文件上运行它并重新加载页面,我还有一个登录控制台进行调试的功能,但它让我无处可去。
请帮忙!
答案 0 :(得分:0)
试试这个
$tag = '5'; // Wrong. This will add the tag with the *name* '5'.
$tag = 5; // Wrong. This will also add the tag with the name '5'.
$tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.
$tag = array( 5 ); // Correct. This will add the tag with the id 5.
wp_set_post_terms( $post_id, $tag, $taxonomy );
答案 1 :(得分:0)
这有帮助,因为在主题函数.php尚未加载的woocommerce分类中,它必须挂在init上。
https://wordpress.stackexchange.com/questions/27111/get-term-by-not-working-when-in-functions-php