如何将类别设置为产品woocommerce [英] How to set category to product woocommerce

查看:30
本文介绍了如何将类别设置为产品woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 woocommerce 帖子中添加类别?

is it possible to add a category to a woocommerce post?

我正在按如下方式创建我的产品:

I am creating my products as follows:

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

我有一个名为树"的类别,我必须将上述产品添加到其中.我尝试了以下但没有成功.有什么特别的方法吗添加类别?

I have a category called "Tree" where I have to add the above product to. I have tried the following but without succes. Is there some special way to add a category?

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

推荐答案

经过反复试验,我解决了以下方法:

After some trial and error I solved it the following way:

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

更多信息参考:

这篇关于如何将类别设置为产品woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆