感谢常网大大,已经明白大致思路了,这是自己感悟的代码,供各位朋友参考
/**
用户购买某个商品自动从vip 0升级到vip n的代码
*/
add_filter('b2_order_callback_gx', 'update_vip', 0,2);
function update_vip($money, $data) {
$user_id = isset($data['user_id']) ? (int)$data['user_id'] : 0;
$post_id = isset($data['post_id']) ? (int)$data['post_id'] : 0;
// 商品的ID
if ($post_id != 8 && $post_id != 15 && $post_id != 16 && $post_id != 29) return $data;
$current_lv = get_user_meta($user_id, 'zrz_vip', true);
$current_lv = $current_lv ? $current_lv : 'vip0';
$update_lv = 'vip0'; // 默认等级为vip0
// 根据当前等级确定要变更的等级
switch ($current_lv) {
case 'vip0':
$update_lv = 'vip1';
break;
case 'vip1':
$update_lv = 'vip2';
break;
case 'vip2':
$update_lv = 'vip3';
break;
case 'vip3':
// 如果已经是最高等级,不需要再升级
return $data;
}
// 重新计算VIP用户数量
$count = get_option('b2_vip_count');
$count = is_array($count) ? $count : array();
if ($current_lv && $update_lv !== $current_lv) {
if (isset($count[$current_lv])) {
$count[$current_lv]--;
}
}
if (isset($count[$update_lv])) {
$count[$update_lv]++;
} else {
$count[$update_lv] = 1;
}
update_option('b2_vip_count', $count);
// 更新到新等级
update_user_meta($user_id, 'zrz_vip', $update_lv);
return $data;
}
你这个代码在现在这个版本里面可以用,但在之前的不实用。之前的版本好像是不支持限制会员等级购买。如果所有会员都能购买这几个商品。每次购买最便宜的那个就能升一个等级。
我用的就是现在的版本b2,不知道子比的嗯么弄
我用的就是现在的版本b2,不知道子比的嗯么弄