View Categories

How to send Referral Codes to Klaviyo

READ FIRST: This code needs to be added at the end of the functions.php file. If you are familiar with editing functions.php, feel free to continue reading. If you haven’t done this before, adding this code in the wrong place might bring the site down and the only way to access it is by editing functions.php file again via FTP. Feel free to contact us if you need help!

If you want to send referral code to Klaviyo on every user registration, add below code. This is working for Referral plugin from version 4.1.4

add_action('wpgens_raf_new_user_referral_id', 'process_new_user_referral', 10, 2);

function process_new_user_referral($referral_id, $user_id) {
    // Get user data
    $user = get_userdata($user_id);
    $email = $user->user_email;

    // Send to Klaviyo
    send_to_klaviyo($email, $referral_id);
}

function send_to_klaviyo($email, $referral_id) {
    // Klaviyo API endpoint for retrieving profile
    $url = 'https://a.klaviyo.com/api/profiles/';

    // Your Klaviyo private API key
    $api_key = 'YOUR-PRIVATE-KEY';

    // Step 1: Retrieve Klaviyo Profile ID
    $response = wp_remote_get($url . "?filter=equals(email,\"" . urlencode($email) . "\")", [
        'headers' => [
            'Authorization' => 'Klaviyo-API-Key ' . $api_key,
            'Accept' => 'application/json',
            'Revision' => '2024-02-15'
        ]
    ]);

    if (is_wp_error($response)) {
        error_log('Failed to retrieve Klaviyo profile: ' . $response->get_error_message());
        return;
    }

    $body = json_decode(wp_remote_retrieve_body($response), true);
    if (empty($body['data'][0]['id'])) {
        error_log('Klaviyo profile not found for email: ' . $email);
        return;
    }

    $klaviyo_user_id = $body['data'][0]['id'];

    // Step 2: Update Klaviyo Profile with Referral ID
    $update_body = json_encode([
        'data' => [
            'type' => 'profile',
            'id' => $klaviyo_user_id,
            'attributes' => [
                'properties' => [
                    'raferral_code' => $referral_id
                ]
            ]
        ]
    ]);

    $update_response = wp_remote_request($url . $klaviyo_user_id . '/', [
        'method' => 'PATCH',
        'headers' => [
            'Authorization' => 'Klaviyo-API-Key ' . $api_key,
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
            'Revision' => '2024-02-15'
        ],
        'body' => $update_body
    ]);

    if (is_wp_error($update_response)) {
        error_log('Failed to update Klaviyo profile: ' . $update_response->get_error_message());
    } else {
        $update_body = wp_remote_retrieve_body($update_response);
        $update_data = json_decode($update_body, true);
        if (isset($update_data['errors'])) {
            error_log('Klaviyo API error: ' . print_r($update_data['errors'], true));
        }
    }
}
What they say

Trusted by 7000+ customers

Our plugins are used by more than 7000 websites across the globe. All of our plugins are rated 5 stars on WordPress.org. Don’t just take our word for it, check what others are saying about WPGens.

I bought and tested the plugin with WooCommerce Subscription and it works like a charm. I had some specific questions to the developer and he responded very fast and helped me. I suggest the plugin!

Refer a Friend PREMIUM
@rinpocse91

If your looking to set up a referral program on your WooCommerce site to boost up your site sale “Refer A Friend for WooCommerce by WPGens” is your plugin to go for. Little blown away with an amazing support provided from (Goran) the plugin working exactly what you want to set up a referral system. Good job guys!

Refer a Friend PREMIUM
@samroon007

We deal with dozens of applications and plugins on a daily basis. Many of them are excellent, but few have truly excellent support. The Refer A Friend plugin is one of them: the response to my questions is always fast, friendly and very helpful.

Refer a Friend PREMIUM
@smartDIYer

This plugin is awesome. It looks good, it’s light weight, has great support – everything you can expect from a paid plugin, only it’s free!!!!

Swifty Bar
@ofirbeigel

Read all testimonials on WordPress Repository.

Get notified when we add new plugins.

We do not publish plugins often, but when we do, it's something awesome. No spam, we promise.