1

I'm trying to integrate klaviyo with woocom form, this is the code I added below. The issue is when I add any hardcoded email in variable $emailUntrim = "xyzxyz@gmail.com" instead of $emailUntrim = $user->user_email; then it works. What I'm trying to achieve is when a new user gets registered in from woocom form they also get added into a list in klaviyo. Any leads would be great regarding the code, it may be not accurate method but I'm open for suggestions. Thank you

{


// Add this function in your functions.php or in your plugin


    $user = wp_get_current_user();
    $emailUntrim = $user->user_email;
    $xxx = '{"profiles":[{"email":"'.$emailUntrim.'"}]}';
    $xxx = json_decode($xxx);
    $yyy = json_encode($xxx);
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://a.klaviyo.com/api/v2/list/XBJzDw/members?api_key=YOUR_Private_Api_key_From_klaviyo",
        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\":\"".$emailUntrim."\"}]}",
        CURLOPT_POSTFIELDS => $yyy,

        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;
    } else {
        echo $response;
    }
}

add_action('woocommerce_created_customer', 'misha_add_register_form_field');
5
  • 1
    If you use something like wp_die($user->user_email); or error_log($user->user_email), do you see a proper email address?
    – kero
    Aug 19 at 15:17
  • @kero if i add wp_die($user->user_email); in the above function it gives {"detail":"email or phone number is a required field for each profile"} error. Is it like not reading the user? Aug 19 at 15:31
  • @kero Do you think it's an issue with the user not being read when the function is called? Aug 19 at 15:48
  • Most likely the user is not logged in (or WP is thinking so). Doesn't the hook give you the $customer_id as first argument, so you could query the user via that?
    – kero
    Aug 20 at 9:31
  • 1
    @kero Ah I found the solution please check the answer if it makes sense, I think I was missing some parameters there at add action. Aug 20 at 20:26
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.