Problem with Updating the DB in FlluentCRM upon Topic Completion in LearnDash

67 Views Asked by At

I'm fairly new to PHP. I have a wordpress site where I use an LMS plugin called LearnDash and a CRM called FluentCRM.

I wrote a code snippet so that when a student finishes a topic in LearnDash it then updates the date of the custom field of that contact in fluentCRM to store the date of completion (I want to have an automation to check whether he/she has progressed more after 7 days and if not send them an email).

But... it doesn't work. I have no idea why. I would greatly appreciate your help. I'm also attaching the links to the relevant documentations:

https://developers.fluentcrm.com/global-functions/

https://developers.learndash.com/

Here's the code:

add_action( 'learndash_topic_completed', 'update_fluentcrm_custom_field_on_topic_completion', 10, 2 );
function update_fluentcrm_custom_field_on_topic_completion( $user_id, $topic_id ) {
    // Initialize FluentCRM API
    $contactApi = FluentCrmApi('contacts');

    try {
    // Retrieve contact using the provided user ID
    $contact = $contactApi->getContactByUserRef($user_id);

    if ($contact) {
        // Extract the email address from the retrieved contact
        $email = $contact->email;

        // Prepare contact data with the extracted email and custom field
        $data = [
            'email' => $email,
            'custom_values' => [
                'utm_content' => 'updated'
            ]
        ];

        // Create or update the contact with the specified data
        $contact = $contactApi->createOrUpdate($data);

        error_log("FluentCRM custom field 'utm_content' updated successfully for contact: $email");
    } else {
        error_log("Contact not found in FluentCRM for user ID: $user_id");
    }
} catch (\Exception $e) {
    error_log("Error updating FluentCRM custom field: " . $e->getMessage());
}

}

0

There are 0 best solutions below