Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 271
Kazik Jarmolovski Avatar asked Jun 27 '13 08:06

Kazik Jarmolovski


People also ask

Where are WooCommerce categories stored?

To start with, the WooCommerce products are mainly stored in two default WordPress tables wp_posts and wp_postmeta.

How the WooCommerce order data is stored in the database?

WooCommerce uses a combination of both WordPress database tables and its own custom tables to store its data. However, WooCommerce doesn't have a dedicated table to store the customer data. The customer data is stored in different database tables, which sometimes might make retrieval of this data challenging.

Where are WordPress categories stored in database?

The categories for both posts and links and the tags for posts are found within the wp_terms table. Each term features information called the meta data and it is stored in wp_termmeta.

Where is WooCommerce product in database?

In conclusion, WooCommerce stores all of its data in the same database as WordPress. You can access this data by logging into your WordPress site and going to the WooCommerce->Settings->Advanced page. Alternatively, you can connect to your WordPress database using a tool like phpMyAdmin.


1 Answers

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;
}
like image 138
Innocent Yash Avatar answered Oct 18 '22 19:10

Innocent Yash