WooCommerce:使用 SQL 查询获取产品类别的购买项目

WooCommerce: Get purchased items for a product category using a SQL query

我想列出属于某个产品类别的所有已购买产品,例如 "clothes" 产品类别下的所有 T 恤订单。

我发现 Woocommerce 数据库结构非常混乱,您必须编写长查询才能获得简单信息。

谁能提供一个示例查询来获取特定类别的所有订单?

我知道这可能涉及 wp_woocommerce_order_itemswp_terms 表,但从那里迷路了。

以下 SQL 查询将为您提供属于 "clothes" 产品类别 slug 的至少一次购买的产品 ID 列表:

SELECT DISTINCT tr.object_id
FROM wp_term_relationships tr
INNER JOIN wp_term_taxonomy tt
    ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms t
    ON tt.term_id = t.term_id
INNER JOIN wp_woocommerce_order_itemmeta oim
    ON tr.object_id = oim.meta_value
INNER JOIN wp_woocommerce_order_items oi
    ON oim.order_item_id = oi.order_item_id
INNER JOIN wp_posts as o
    ON oi.order_id = o.ID
WHERE tt.taxonomy = 'product_cat'
    AND t.slug = 'clothes'
    AND oim.meta_key = '_product_id'
    AND o.post_type = 'shop_order'
    AND o.post_status IN ('wc-completed','wc-processing')

已测试并有效

或者在 Wordpress 中使用 WPDB class 这可以通过以下方式完成:

global $wpdb;

$product_category = 'clothes'; // Term slug

$products_ids = $wpdb->get_col( "
    SELECT DISTINCT tr.object_id
    FROM wp_term_relationships tr
    INNER JOIN wp_term_taxonomy tt
        ON tr.term_taxonomy_id = tt.term_taxonomy_id
    INNER JOIN wp_terms t
        ON tt.term_id = t.term_id
    INNER JOIN wp_woocommerce_order_itemmeta oim
        ON tr.object_id = oim.meta_value
    INNER JOIN wp_woocommerce_order_items oi
        ON oim.order_item_id = oi.order_item_id
    INNER JOIN wp_posts as o
        ON oi.order_id = o.ID
    WHERE tt.taxonomy = 'product_cat'
    AND t.slug = '$product_category'
    AND oim.meta_key = '_product_id'
    AND o.post_type = 'shop_order'
    AND o.post_status IN ('wc-completed','wc-processing')
");

// Pre-formatted raw display (an array of products ids)
echo '<pre>'; print_r($products_ids); echo '</pre>';

不是SQL,而是通过 wp 查询。查找所有订单并收集具有所需类别的订单。使用 post__in 属性创建新参数。

$m_args_all = array(
    'orderby' => 'date',
    'posts_per_page' => 12,
);

$ordr_cat = 654 // id of required cat
$empty_cat = false;

$orders_all_raw = wc_get_orders( $m_args_all ); //find all orders by your args
if (!empty($orders_all_raw)) { 
    $orders_in_cat = []; 
    foreach ($orders_all_raw as $order_raw) {
        $items = $order_raw->get_items(); //get products of order
        if (!empty($items)) { 
            foreach ($items as $item_id => $item) { //loop for products
                $term_ids = wp_get_post_terms($item->get_product_id(),  'product_cat', array('fields' => 'ids')); // get all id of cats in product
                if (in_array($ordr_cat, $term_ids)) { // if required cat in array of gethered cats add order id to array
                    if (!in_array($order_raw->ID, $orders_in_cat)) {
                        $orders_in_cat[] = $order_raw->ID; //add order with required prod cat to array 
                    }
                }
            }
        }

    }
    if (empty($orders_in_cat)){
        $empty_cat = true;
    }
    else {
        $m_args_all += array('post__in' => $orders_in_cat);
    }

}
if($empty_cat){
    $orders = [];
}
else{
    $orders = wc_get_orders( $m_args_all );
}