WooCommerce的类别和子类别如何保存在数据库中? [英] How do categories and subcategories for WooCommerce are saved in DB?

查看:297
本文介绍了WooCommerce的类别和子类别如何保存在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WordPress插件-WooCommerce中存在类别和子类别的问题. 我正在创建一个将创建类别和子类别的脚本,问题是我不完全了解WooCommerce 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.

这就是我能够做到的:

在"wp_terms"中:

In "wp_terms":

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

在"wp_term_taxonomy"中:

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:

在"wp_term_taxonomy"中:

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

推荐答案

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

这篇关于WooCommerce的类别和子类别如何保存在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆