Hey guys there are just a few changes in the above code. Check this out it works for me.
Note: Please change the LIST_ID to your klaviyo’s list id, and get one private key from your klaviyo account and add it at PRIVATE_API_KEY
function send_coupon_to_freshly_registered_user($user_id) {
$user = get_user_by('id',$user_id); //new line
$user_email = stripslashes($user->user_email); //changed line
$emailecho = "".$user_email."";
$xxx = '{"profiles":[{"email":"'.$emailecho.'"}]}';
$xxx = json_decode($xxx);
$yyy = json_encode($xxx);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://a.klaviyo.com/api/v2/list/LIST_ID/members?api_key=PRIVATE_API_KEY",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"profiles\":[{\"email\":\"".$emailecho."\"}]}",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err . "cmdskcmdsl";
} else {
echo $response;
}
}
add_action('user_register', 'send_coupon_to_freshly_registered_user', 10, 1);