虽然mysql中的wp_users和wp_wc_customer_lookup表中都存在数据,但我不确定这段代码有什么问题。在用户注册并更新我的自定义表后,我使用下面的 user_registration 挂钩从“wc_customer_lookup”获取 customer_id。请帮忙,请参考下面提供的代码。
add_action('user_register', 'map_customer_user', 10, 2);
function map_customer_user($user_id)
{
if (isset($_COOKIE["uuid_custom"])) {
global $wpdb;
// Sanitize user input
$user_id = intval($user_id);
$table_name = $wpdb->prefix . 'custom_Table';
// Use prepare() to avoid SQL injection
$customer = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM " . $wpdb->prefix . "wc_customer_lookup WHERE user_id = %d",
$user_id
)
);
if ($customer) {
$data_update = array(
'status' => 2,
'customer' => $customer->customer_id
);
$data_where = array('customer_cookie_id' => $_COOKIE["uuid_custom"]);
// Use $wpdb->update() to update data
$wpdb->update($table_name, $data_update, $data_where);
// Unset the cookie
unset($_COOKIE["uuid_custom"]);
setcookie('uuid_custom', '', time() - 3600, '/');
} else {
// Handle case where customer data is not found
}
}
}
检查 wc_customer_lookup 表是否正在使用:
保证待遇在客户招募前设定:
调试:
//调试语句示例 error_log('用户 ID: ' . $user_id);
数据库前缀:
add_action('user_register', 'map_customer_user', 10, 1);
函数map_customer_user($user_id) { if (isset($_COOKIE["uuid_custom"])) {
global $wpdb;
// Sanitize user input
$user_id = intval($user_id);
// Use prepare() to avoid SQL injection
$customer = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}wc_customer_lookup WHERE user_id = %d",
$user_id
)
);
if ($customer) {
$table_name = $wpdb->prefix . 'custom_Table';
$data_update = array(
'status' => 2,
'customer' => $customer->customer_id
);
$data_where = array('customer_cookie_id' => $_COOKIE["uuid_custom"]);
// Use $wpdb->update() to update data
$wpdb->update($table_name, $data_update, $data_where);
// Unset the cookie
unset($_COOKIE["uuid_custom"]);
setcookie('uuid_custom', '', time() - 3600, '/');
} else {
// Handle case where customer data is not found
error_log('Customer not found for user ID: ' . $user_id);
}
}
}