如何在数据库中保存Woocommerce的类别和子类别?

Kaz*_*ski 9 mysql sql database wordpress woocommerce

我在Wordpress插件中的类别和子类别有问题 - Woocommerce.我正在创建一个可以创建类别和子类别的脚本,问题是我不完全理解Woocommerce DB结构中的所有这些是如何工作的.

这就是我能做到的:

在"wp_terms"中:

term_id | name              | slug     | term group
20      | Parent category   | parent   | 0
21      | Children category | children | 0
Run Code Online (Sandbox Code Playgroud)

在"wp_term_taxonomy"中:

term_taxonomy_id | term_id | taxonomy    | description | parent | count
1                | 20      | product_cat |             | 0      | 0
2                | 21      | product_cat |             | 20     | 0
Run Code Online (Sandbox Code Playgroud)

这是有效的,但为什么不这样做:

在"wp_term_taxonomy"中:

term_taxonomy_id | term_id | taxonomy    | description | parent | count
1                | 20      | product_cat |             | 21     | 0
2                | 21      | product_cat |             | 0      | 0
Run Code Online (Sandbox Code Playgroud)

任何线索?

非常感谢大家提前.:)

小智 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意代码的格式。 (3认同)

小智 0

也许因为一个是父母,另一个是孩子,所以如果你定义这种关系,那么相反的关系就不应该成立。