简体   繁体   English

如何在woocommerce wordpress中添加产品类别

[英]How to add a product category in woocommerce wordpress

I really hope someone can help me with this problem asap.我真的希望有人能尽快帮助我解决这个问题。

Ok so I am building a costum script for users to publish a new product, I have everything working and inserting successfully (Event the photo) but just cant seem to find anywhere what code should be used to update the post category, as it is not a normal category because it has the taxonomy of "product_cat" (woocommerce product category).好的,所以我正在为用户构建一个用于发布新产品的 Costum 脚本,我让一切正常工作并成功插入(事件照片)但似乎无法在任何地方找到应该使用什么代码来更新帖子类别,因为它不是一个普通类别,因为它具有“product_cat”(woocommerce 产品类别)的分类法。

Any ideas?有任何想法吗?

Non of the following work: ($term_id is the term_id that relates to the "product_cat" of a certain product)非以下工作:($term_id 是与某个产品的“product_cat”相关的 term_id)

wp_set_post_terms($post_id, array($term_id), "product_cat");
    wp_set_post_terms($post_id, (int)$term_id, "product_cat");
    update_post_meta($post_id,'product_cat',$term_id);

I have tried others but they just dont seem to do anything at all, some functions even create a new category with the id...我试过其他人,但他们似乎根本没有做任何事情,有些功能甚至创建了一个带有 id 的新类别......

You can use:您可以使用:
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )

Parameters:参数:

$post_id (int) (Optional) The Post ID. $post_id (int) (可选)帖子 ID。 Does not default to the ID of the global $post.不默认为全局 $post 的 ID。

$tags (string|array) (Optional) An array of terms to set for the post, or a string of terms separated by commas. $tags (string|array) (可选)要为帖子设置的术语数组,或用逗号分隔的术语字符串。 Hierarchical taxonomies must always pass IDs rather than names so that children with the same names but different parents aren't confused.分层分类法必须始终传递 ID 而不是名称,以便具有相同名称但不同父母的孩子不会混淆。 Default value: ''默认值: ''

$taxonomy (string) (Optional) Taxonomy name. $taxonomy(字符串)(可选)分类名称。

Default value: 'post_tag'默认值:'post_tag'

$append (bool) (Optional) If true, don't delete existing terms, just add on. $append (bool) (可选)如果为 true,则不要删除现有术语,只需添加即可。 If false, replace the terms with the new terms.如果为 false,则用新条款替换条款。

Default value: false默认值:false

Read more about it here: https://developer.wordpress.org/reference/functions/wp_set_post_terms/在此处阅读更多相关信息: https : //developer.wordpress.org/reference/functions/wp_set_post_terms/

Try with wp_insert_term :尝试使用wp_insert_term

wp_insert_term(
  'New Category', // the term 
  'product_cat', // the taxonomy
  array(
    'description'=> 'Category description',
    'slug' => 'new-category'
  )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM