如何将类别和子类别添加到woocommerce产品?

问题描述:

我正在创建一个插件以将产品添加到woocommerce 和$th1是类别,$sth1是子类别。如何将类别和子类别添加到woocommerce产品?

我试过,但它不工作:

wp_set_object_terms($post_id,$th1->$sth1,'product_cat'); 

我想增加产品的类别和子类别。 请问我该怎么做?

提供你有term_id是为了你们的类别(即与get_term_by),你可以这样做:

wp_set_object_terms($post_id, [$category->term_id, $sub_category->term_id], 'product_cat'); 

您需要的产品ID添加到表term_relationships。为此,您需要从“term_taxonomy”表中获得“term_taxonomy_id”。

因此,在term_relationships中,object_id =从term_taxonomy表中找到的product_ID和term_taxonomy_id。

您可以通过这种方式为产品分配任何类别/子类别。