How do categories and subcategories for Woocommerc

2019-04-24 10:40发布

I have a problem with categories and subcategories in Wordpress plugin - Woocommerce. I'm creating a script which would create a categories and subcategories, the problem is that I don't fully understand how all this works in Woocommerce DB structure.

That's what I was able to do:

in "wp_terms":

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

in "wp_term_taxonomy":

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

And that's working, but why the hell this don't:

in "wp_term_taxonomy":

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

Any leads?

Big thanks for everyone in advance. :)

3条回答
Rolldiameter
2楼-- · 2019-04-24 11:21
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;
}
查看更多
迷人小祖宗
3楼-- · 2019-04-24 11:27

Maybe because one is the parent and the other the child, so if you define this relation, it's right that the opposite shouldn't work.

查看更多
别忘想泡老子
4楼-- · 2019-04-24 11:39

You have to look into table wp_options for option_name "product_cat_children", there is serialized category hierarchy.

查看更多
登录 后发表回答