Kazik Jarmolovski
Kazik Jarmolovski

Reputation: 211

How do categories and subcategories for WooCommerce are saved in DB?

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

Upvotes: 11

Views: 27689

Answers (5)

Dmitriy Fedotov
Dmitriy Fedotov

Reputation: 1

Once I had a problem: Subcategories were not displayed, despite the fact that both databases were filled correctly. The following helped:

  • Delete the value of the product_cat_children field in the wp_options table
  • Update key values via admin panel in WooCommerce

This will update the value of the product_cat_children field.

Everything worked.

Upvotes: 0

Ton
Ton

Reputation: 1

You need one more step : add your catoegory to the right products in : wp_term_relationships

Upvotes: 0

Marek Paral
Marek Paral

Reputation: 31

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

Upvotes: 3

Innocent Yash
Innocent Yash

Reputation: 91

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;
}

Upvotes: 9

Almakantara
Almakantara

Reputation: 1

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.

Upvotes: 0

Related Questions