How are categories and subcategories for Woocommerce stored in the database?

I have a problem with categories and subcategories in the Wordpress plugin - Woocommerce. I am creating a script that will create categories and subcategories, the problem is that I do not quite understand how it all works in the Woocommerce database structure.

What could I 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 it works, but why the hell do not:

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 conclusions?

Thanks so much for everyone in advance. :)

+6
source share
3 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; } 
+6
source

Perhaps because one is a parent and the other is a child, therefore, if you define this relationship, it is correct that the opposite should not work.

0
source

You should look in the wp_options table for option_name "product_cat_children", there is a hierarchy of serialization categories.

0
source

Source: https://habr.com/ru/post/948203/


All Articles