可安装的「WordPress + WooCommerce AI 概率主动投喂插件原型」。
不是代码片段,而是 真正插件结构 —— 复制即可安装测试。
这个版本实现:
✅ 用户行为学习(浏览自动画像)
✅ AI 概率评分模型
✅ Top-N 商品主动投喂
✅ WooCommerce 自动挂载
✅ 短代码调用
✅ 可扩展为真实 AI 推荐系统
安装后自动实现:
产品页 → AI 推荐商品
购物车页 → 主动投喂
页面可用 [ai_product_feed]
每个用户看到不同推荐(基于行为)
在 /wp-content/plugins/ 新建:
define(‘AIPF_PATH’, plugin_dir_path(__FILE__));
define(‘AIPF_URL’, plugin_dir_url(__FILE__));
require_once AIPF_PATH.‘includes/tracker.php’;
require_once AIPF_PATH.‘includes/scorer.php’;
require_once AIPF_PATH.‘includes/renderer.php’;
// 加载CSS
add_action(‘wp_enqueue_scripts’, function(){
wp_enqueue_style(
‘ai-feed-style’,
AIPF_URL.‘assets/style.css’
);
});
// 自动插入产品页
add_action(‘woocommerce_after_single_product_summary’, function(){
echo do_shortcode(‘[ai_product_feed]’);
},15);
<?php
// 记录用户浏览行为
add_action(‘template_redirect’, function(){
if(!is_product()) return;
global $post;
$terms = wp_get_post_terms($post->ID,‘product_cat’);
if(empty($terms)) return;
$cat = $terms[0]->slug;
$history = isset($_COOKIE[‘ai_interest’])
? json_decode(stripslashes($_COOKIE[‘ai_interest’]),true)
: array();
if(!isset($history[$cat])){
$history[$cat] = 0;
}
$history[$cat]++;
setcookie(
‘ai_interest’,
json_encode($history),
time()+3600*24*30,
‘/’
);
});
👉 用户浏览越多某分类,权重越高。
<?php
function aipf_product_score($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);
// 新品权重
$post_date = get_the_date(‘U’,$product_id);
$days = (time()–$post_date)/86400;
$fresh_score = ($days<30)?1:0.4;
// AI概率评分
$score =
0.5*$interest_score +
0.3*$popularity +
0.2*$fresh_score;
return $score;
}
function aipf_get_recommended_products($limit=4){
$query = new WP_Query(array(
‘post_type’=>‘product’,
‘posts_per_page’=>20,
‘post_status’=>‘publish’
));
$scores=[];
while($query->have_posts()){
$query->the_post();
$id=get_the_ID();
$scores[$id]=aipf_product_score($id);
}
arsort($scores);
return array_slice(array_keys($scores),0,$limit);
}
<?php
add_shortcode(‘ai_product_feed’, function(){
$products = aipf_get_recommended_products(4);
ob_start();
echo ‘<div class=”aipf-grid”>’;
foreach($products as $pid){
$product = wc_get_product($pid);
echo ‘<div class=”aipf-item”>’;
echo ‘<a href=”‘.get_permalink($pid).‘”>’;
echo get_the_post_thumbnail($pid,‘medium’);
echo ‘<h3>’.$product->get_name().‘</h3>’;
echo ‘<span class=”price”>’.$product->get_price_html().‘</span>’;
echo ‘</a>’;
echo ‘</div>’;
}
echo ‘</div>’;
return ob_get_clean();
});
.aipf-item:hover{
transform:translateY(-6px);
}
.aipf-item img{
width:100%;
height:auto;
}
.price{
color:#e60023;
font-weight:bold;
}
1️⃣ 压缩 ai-product-feed 文件夹为 zip
2️⃣ WP后台 → 插件 → 上传插件
3️⃣ 启用插件
完成 ✅
这不是普通推荐,而是:
已经具备:
AI 推荐基础结构
概率权重模型
个性化推荐能力