如何将所有woocommerce产品类别划分为单独的mysql表?
答案 0 :(得分:0)
使用此代码。注意:听起来你正试图从同一个表中的不同行获取几个数据。
$sql = "SELECT p.id, p.post_title, p.guid, p.post_type, m.meta_key, m.meta_value, meta_sp.meta_value as sale_price, meta_ap.meta_value as additional_price
FROM wp_posts p
INNER JOIN wp_postmeta m
INNER JOIN wp_postmeta meta_sp ON p.id=meta_sp.post_id
AND meta_sp.meta_key='sale_price'
INNER JOIN wp_postmeta meta_ap ON p.id=meta_ap.post_id
AND meta_ap.meta_key='additional_price'
WHERE p.id=m.post_id
AND m.meta_key='_rentable' AND m.meta_value='yes'
";
答案 1 :(得分:0)
我正在使用代码方式bcoz我没有找到任何特定的解决方案与mysql查询,因为它的3级深度类别。它的工作和测试代码。
$all_categories = get_categories(array("taxonomy" => "product_cat", "parent" => 0, "show_count" => true));
foreach ($all_categories as $cat) {
$category_id = $cat->term_id;
$sub = get_categories(array("taxonomy" => "product_cat", "parent" => $category_id));
$wpdb->query( $wpdb->prepare( "INSERT INTO product_category(cat_id, cat_name, parent_id)
VALUES ( %d, %s, %d )", array($category_id, $cat->name, $cat->category_parent)));
if(!empty($sub)){
foreach($sub as $subcat){
if($subcat->category_parent != ''){
$subcatid = $subcat->term_id;
$wpdb->query( $wpdb->prepare( "INSERT INTO product_category(cat_id, cat_name, parent_id)
VALUES ( %d, %s, %d )", array($subcatid, $subcat->name, $subcat->category_parent)));
}
$thirdlevel = get_categories(array("taxonomy" => "product_cat", "parent" => $subcatid));
if(!empty($thirdlevel)){
foreach($thirdlevel as $thirdcat){
if($thirdcat->category_parent != ''){
$lastcatid = $thirdcat->term_id;
$thirdarray = array('cat_id'=> $lastcatid, 'cat_name'=>$thirdcat->name, 'parent_id'=> $thirdcat->category_parent);
$wpdb->query( $wpdb->prepare( "INSERT INTO product_category(cat_id, cat_name, parent_id)
VALUES ( %d, %s, %d )", array($lastcatid, $thirdcat->name, $thirdcat->category_parent)));
}
}
}
}
}
}
在这些代码之后,您可以使用此查询从新表中获取所有数据。
SELECT t1.cat_id AS lev1,t1.cat_name AS lev1name,t2.cat_id AS cat2,t2.cat_name AS cat2name,t3.cat_id AS lev3,t3.cat_name AS lev4 FROM product_category AS t1 LEFT JOIN product_category AS t2 ON t2.parent_id = t1.cat_id LEFT JOIN product_category AS t3 ON t3.parent_id = t2.cat_id