我正在尝试让我的代码根据用户是否有 10 个或更多正在处理/已完成的订单来更改用户角色。它检查买家的电子邮件以将其与其他订单相匹配。尝试下新订单时出现内部服务器错误,我尝试了 servral 解决方案但没有任何运气。
add_action('woocommerce_new_order', 'change_user_role_based_on_order_count', 10, 1);
function change_user_role_based_on_order_count($order_id) {
// Get the order object
$order = wc_get_order($order_id);
// Get the user email associated with the order
$user_email = $order->get_billing_email();
// Exclude administrators from role change
if (email_exists($user_email) && user_can(email_exists($user_email), 'administrator')) {
return; // Exit the function
}
// Get the user ID associated with the order email
$user_id = email_exists($user_email);
// Get the user's current role
$current_user_role = get_user_role($user_id);
// Define the desired role
$desired_role = 'your_desired_role';
// Check if the user has more than 10 orders and is not already in the desired role
if (get_user_order_count($user_id) > 10 && $current_user_role !== $desired_role) {
// Update the user's role to the desired role
wp_update_user(array('ID' => $user_id, 'role' => $desired_role));
}
}