Site icon Hip-Hop Website Design and Development

woocommerce registration form with klaviyo(don’t work with current user)

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