Woocommerce 产品类别术语是 WordPress 自定义分类法product_cat...
在数据库中数据也位于表wp_terms、wp_term_taxonomy、wp_termmeta 和wp_term_relationships 下。
1) 要以编程方式添加新的产品类别术语,您将使用专用的 WordPress 功能 wp_insert_term(),例如:
// Adding the new product category as a child of an existing term (Optional)
$parent_term = term_exists( 'fruits', 'product_cat' ); // array is returned if taxonomy is given
$term_data = wp_insert_term(
'Apple', // the term
'product_cat', // the Woocommerce product category taxonomy
array( // (optional)
'description'=> 'This is a red apple.', // (optional)
'slug' => 'apple', // optional
'parent'=> $parent_term['term_id'] // (Optional) The parent numeric term id
)
);
这将返回一个包含 term Id 和 术语分类 ID 的数组,例如:
array('term_id'=>12,'term_taxonomy_id'=>34)
2) 菜单顺序: 要设置甚至更改产品类别的菜单顺序,您将使用add_term_meta() Wordpress 功能。
您将需要您的产品类别的术语 ID 和唯一的订购数值(此处以 2 为例):
add_term_meta( $term_data['term_id'], 'order', 2 );
3) 缩略图:您还将使用add_term_meta() 将缩略图 ID 设置为使用类似 的产品类别(其中最后一个参数是数字缩略图 ID 参考) em>:
add_term_meta( $term_data['term_id'], 'thumbnail_id', 444 );
4) 在产品中设置产品类别:
现在要将此新产品类别“Apple”设置为现有产品 ID,您将使用类似 (从新创建的“Apple”产品类别生成相应的 $term_id) :
wp_set_post_terms( $product_id, array($term_data['term_id']), 'product_cat', true );
供参考:函数wp_set_post_terms()