infusionsoft / infusionsoft-php

PHP client library for the Infusionsoft API.
https://developer.infusionsoft.com/
Other
129 stars 126 forks source link

How to update infusionsoft contact's custom fields using php-sdk API? #284

Closed RanaDante closed 3 years ago

RanaDante commented 3 years ago

I'm working on a custom solution to update infusionsoft contact's custom fields data using PHP SDK, here is what I've done so far.

global $wpdb;
$keap_token_table = $wpdb->prefix . "keap_token";

$infusionsoft = new \Infusionsoft\Infusionsoft(array(
    'clientId'     => 'clientID',
    'clientSecret' => 'clientSecret',
    'redirectUri'  => home_url(),
));

$results = $wpdb->get_results("SELECT token_id, keap_token, refresh_token FROM {$keap_token_table} ORDER BY token_id DESC LIMIT 1",'ARRAY_A');

$token_id = $results[0]['token_id'];

$keap_token = $results[0]['keap_token'];
$keap_token = unserialize($keap_token);

if($keap_token) {
    $infusionsoft->setToken($keap_token);
    $refreshed_token = $infusionsoft->refreshAccessToken();
    $data_update = array('keap_token' => serialize($refreshed_token));
    $data_where = array('token_id' => $token_id);
    $wpdb->update($keap_token_table , $data_update, $data_where);

    $registered_user = $infusionsoft->contacts()->where(array('email'=>$billing_email, 'limit' => '1'))->get();

    if(!$registered_user->isEmpty()) {

        $contact_id = $registered_user->get(0)->getAttribute('id');

        if($registered_user->get(0)->getAttribute('email_addresses')[0]['email'] === $billing_email) {
            if($to_be_included === "1") {
                // $contact_update = $infusionsoft->contacts()->mock(['id' => $contact_id, '_ToBeIncludedInDirectory' => '1'])->save();
            }
        }
    }
}

Here are the few solutions that I've tried by searching on google, but none of them seems to be working for me.


$data = [
                    '_ToBeIncludedInDirectory' => '1',
                    '_DirectoryVisibilityStatus' => $visibility_status,
                    '_MemberDirectoryAddress' => $directory_address,
                    '_AreaOfSpeciality' => 'None'
                ];
                $contact_update = $infusionsoft->update($contact_id, $data);

and

$contact_update = $infusionsoft->contacts()->mock(
                    [
                        'id' => $contact_id,
                        "custom_fields" => [
                            {
                                "contents": {
                                    '_ToBeIncludedInDirectory' => '1',
                                    '_DirectoryVisibilityStatus' => $visibility_status,
                                    '_MemberDirectoryAddress' => $directory_address,
                                    '_AreaOfSpeciality' => 'None'
                                },
                                "id": $contact_id
                            }
                        ]
                    ]
                )->save();

I want to update the custom fields only and after I update them I want to fetch them and display them on the website. Thanks In Advance.

Ultimater commented 3 years ago

Instead of

$contact_update = $infusionsoft->update($contact_id, $data);

try:

$contact_update = $infusionsoft->contacts()->update($contact_id, $data);
RanaDante commented 3 years ago

I tried the provided approach but unfortunately, it didn't work, but I figured out how to update the custom fields, I used

$custom_field_data = [ '_ToBeIncludedInDirectory' => '1', '_DirectoryVisibilityStatus' => $visibility_status, '_MemberDirectoryAddress' => $directory_address, '_AreaOfSpeciality' => 'None']; $contact_update = $infusionsoft->data()->update("Contact", $contact_id, $custom_field_data);

this approach worked perfectly, the issue is resolved now. Thanks for your help.

On Wed, Jun 30, 2021 at 12:31 PM Kevin Yarmak @.***> wrote:

Instead of

$contact_update = $infusionsoft->update($contact_id, $data);

try:

$contact_update = $infusionsoft->contacts()->update($contact_id, $data);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/infusionsoft/infusionsoft-php/issues/284#issuecomment-871165598, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF7QCDOM7HFQ247AO3SIDWDTVLB4JANCNFSM47LVA4XA .