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 = [email protected] 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');

Topic integration-tests json php api forms Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.