// =============================
// AI 商品评分算法
// =============================
function ai_product_score($product_id){
$product = wc_get_product($product_id);
// ① 用户兴趣权重
$interest_data = isset($_COOKIE[‘ai_interest’])
? json_decode(stripslashes($_COOKIE[‘ai_interest’]), true)
: array();
$terms = wp_get_post_terms($product_id,’product_cat’);
$interest_score = 0.3;
if(!empty($terms)){
$slug = $terms[0]->slug;
if(isset($interest_data[$slug])){
$interest_score = min(1, $interest_data[$slug] / 5);
}
}
// ② 商品热度(销量)
$sales = (int)get_post_meta($product_id,’total_sales’,true);
$popularity = min(1, $sales / 100);
// ③ 转化信号(加入购物车次数,可自定义)
$cart_signal = 0.5;
// ④ 新品权重
$post_date = get_the_date(‘U’,$product_id);
$days = (time() – $post_date)/86400;
$fresh_score = ($days < 30) ? 1 : 0.4;
// ===== AI SCORE =====
$score =
0.45*$interest_score +
0.25*$popularity +
0.20*$cart_signal +
0.10*$fresh_score;
return $score;
}