You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中获取客户最常购买的产品ID?

获取WooCommerce客户最常购买的产品ID

当然可行!在WooCommerce里完全能实现这个需求,下面给你两种实用的实现方案,你可以根据自己的场景选择:

方案一:直接数据库查询(高效优先)

这种方式直接操作WooCommerce的订单数据表,性能更优,适合客户订单量较大的场景:

function get_customer_top_product_ids( $customer_id, $limit = 5 ) {
    global $wpdb;
    
    // 只统计已支付、已完成这类有效订单,排除取消/退款的
    $valid_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
    $status_clause = "'" . implode( "','", $valid_statuses ) . "'";
    
    // 查询并统计客户购买各产品的次数
    $product_counts = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT
                oi.product_id,
                COUNT(oi.product_id) AS purchase_count
            FROM {$wpdb->prefix}woocommerce_order_items oi
            LEFT JOIN {$wpdb->prefix}posts p ON oi.order_id = p.ID
            WHERE
                p.post_type = 'shop_order'
                AND p.post_status IN ($status_clause)
                AND p.post_author = %d
            GROUP BY oi.product_id
            ORDER BY purchase_count DESC
            LIMIT %d",
            $customer_id,
            $limit
        ),
        ARRAY_A
    );
    
    // 提取排序后的产品ID数组
    return array_column( $product_counts, 'product_id' );
}

// 使用示例:获取当前登录客户的前5个高频购买产品ID
if ( is_user_logged_in() ) {
    $current_user_id = get_current_user_id();
    $top_products = get_customer_top_product_ids( $current_user_id, 5 );
    
    if ( ! empty( $top_products ) ) {
        echo '<h3>你最常买的产品ID:</h3>';
        echo '<ul>';
        foreach ( $top_products as $product_id ) {
            echo '<li>' . esc_html( $product_id ) . '</li>';
        }
        echo '</ul>';
    } else {
        echo '<p>暂时还没有购买记录哦~</p>';
    }
}

方案二:用WooCommerce内置函数(框架友好)

如果你更倾向于遵循WooCommerce的开发规范,避免直接操作数据库,可以用这种方式:

function get_customer_top_products_via_wc( $customer_id, $limit = 5 ) {
    $product_counts = array();
    
    // 获取客户的所有有效订单
    $customer_orders = wc_get_orders( array(
        'customer_id' => $customer_id,
        'status' => wc_get_is_paid_statuses(),
        'limit' => -1, // 拉取所有订单
    ) );
    
    // 遍历订单,统计每个产品的购买次数
    foreach ( $customer_orders as $order ) {
        foreach ( $order->get_items() as $item ) {
            $product_id = $item->get_product_id();
            $product_counts[$product_id] = isset( $product_counts[$product_id] ) ? $product_counts[$product_id] + 1 : 1;
        }
    }
    
    // 按购买次数降序排序
    arsort( $product_counts );
    
    // 截取前N个高频产品ID
    return array_slice( array_keys( $product_counts ), 0, $limit );
}

// 使用示例:查询指定客户ID的前3个高频产品
$target_customer_id = 123; // 替换成你要查询的客户ID
$top_products = get_customer_top_products_via_wc( $target_customer_id, 3 );

print_r( $top_products );

一些小提示

  • 两种方案默认只统计已支付/已完成的订单,如果你需要包含处理中、待付款等状态,可以修改status参数,比如添加'processing'或'on-hold'。
  • 如果客户订单特别多,建议添加date_created参数限制时间范围(比如只查近一年的订单),避免拖慢网站速度。
  • 管理员可以通过修改$customer_id查询任意客户的记录,普通用户只能查看自己的,记得做好权限控制哦。

内容的提问来源于stack exchange,提问作者Nikunj Kathrotiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:32:00