9

我对 WordPress 插件 - WooCommerce 中的类别和子类别有疑问。我正在创建一个脚本来创建一个类别和子类别,问题是我不完全理解这一切在 WooCommerce DB 结构中是如何工作的。

这就是我能够做到的:

在“wp_terms”中:

term_id | name              | slug     | term group
20      | Parent category   | parent   | 0
21      | Children category | children | 0

在“wp_term_taxonomy”中:

term_taxonomy_id | term_id | taxonomy    | description | parent | count
1                | 20      | product_cat |             | 0      | 0
2                | 21      | product_cat |             | 20     | 0

这行得通,但是为什么这不行:

在“wp_term_taxonomy”中:

term_taxonomy_id | term_id | taxonomy    | description | parent | count
1                | 20      | product_cat |             | 21     | 0
2                | 21      | product_cat |             | 0      | 0
4

4 回答 4

8
function getParentCategories() {
    global $wpdb;
    $sql = "SELECT term_id as id, name, slug FROM wp_terms where term_id in (SELECT term_id FROM wp_term_taxonomy where parent = 0 and taxonomy = 'category') order by name asc";
    $parents = $wpdb->get_results( $sql );
    return $parents;
}

function getChildCategories($id) {
    global $wpdb;
    $sql = "SELECT term_id as id, name, slug FROM wp_terms where term_id in (SELECT term_id FROM wp_term_taxonomy where parent = $id and taxonomy = 'category')  order by name asc";
    $children = $wpdb->get_results( $sql );
    return $children;
}
于 2014-06-06T07:37:43.620 回答
2

您必须查看选项名称“product_cat_children”的表 wp_options,有序列化的类别层次结构。

于 2015-10-27T14:41:28.903 回答
0

也许是因为一个是父母,另一个是孩子,所以如果你定义这种关系,那么相反的情况是正确的。

于 2013-12-27T00:45:25.557 回答
0

您还需要一步:将您的类别添加到正确的产品中:wp_term_relationships

于 2019-11-16T09:45:28.597 回答