I’m creating a plugin that creates a contact in Less Annoying CRM when a user account on a WordPress site is approved. I’ve looked through the API documentation and can’t find anything on how to add a user to a group.
So far I’ve managed to create contact’s and add them to a company if the company already exists, if not it creates a new one and assigns them to it.
I’ve tried adding the user to a buying group but that’s not the same thing, if you manually do it, you have to attach an item to the contact then select group.
Here is my code:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Davora_Lacrm_API {
private $api_url = "https://api.lessannoyingcrm.com/v2/";
private $api_key = "**********************************";
// Standard call for all LACRM API functions
public function call_lacrm_api( string $function, array $parameters = [] ) {
$curl_handle = curl_init( $this->api_url );
$headers = [
"Content-Type: application/json",
"Authorization: $this->api_key"
];
$body = [
"Function" => $function,
"Parameters" => $parameters
];
curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, json_encode( $body ) );
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
$curl_result = curl_exec( $curl_handle );
$return_value = false;
// Handle errors
if ( curl_errno( $curl_handle ) ) {
error_log( "cURL Error: " . curl_error( $curl_handle ) );
} else {
$result_object = json_decode($curl_result, true);
$http_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
if ($http_code === 400) {
error_log( "LACRM API Error: " . $result_object['ErrorDescription'] );
} else {
$return_value = $result_object;
}
}
curl_close( $curl_handle );
return $return_value;
}
// Check if a company exists in LACRM by searching contacts
public function check_if_company_exists( $company_name ) {
// Use GetContacts to search for the company (which is a contact with IsCompany = true)
$response = $this->call_lacrm_api( 'GetContacts', [
"SearchTerms" => $company_name, // Search by company name
"AdvancedFilters" => [
[
"Name" => "CompanyName", // Search in the "Company" field
"Operation" => "IsExactly", // Use "IsExactly" to match the exact company name
"Value" => $company_name
]
]
]);
// Log the response for debugging
if ( $response ) {
error_log("Check Company Response: " . print_r( $response, true ));
} else {
error_log("No response from LACRM API while checking for company: $company_name");
}
// Check if we have a valid response with matching contacts
if ( $response && isset( $response['Results'] ) && count( $response['Results'] ) > 0 ) {
foreach ( $response['Results'] as $contact ) {
// Manually check if this contact is a company
if ( isset( $contact['IsCompany'] ) && $contact['IsCompany'] == 1 ) {
return $contact; // Return the first company (contact) found
}
}
}
return false; // Company does not exist
}
// Create a new company as a contact in LACRM
public function create_company( $company_name ) {
$parameters = [
"IsCompany" => true, // Set IsCompany to true to create a company
"Company Name" => $company_name, // Correctly reference the "Company Name" field
"AssignedTo" => 555885 // Ensure assigned to a user
];
// Create the company
$response = $this->call_lacrm_api( "CreateContact", $parameters );
// Log the response for debugging
if ( $response ) {
error_log("Company created successfully: " . print_r( $response, true ));
return $response; // Return the response which will contain the ContactId
} else {
error_log("Failed to create company: $company_name");
return false;
}
}
public function prepare_lacrm_parameters( $user_id ) {
$user = get_userdata( $user_id );
// Get the company name from user meta
$company_name = get_user_meta( $user_id, 'billing_company', true );
if ( ! $company_name ) {
$company_name = "New Company"; // Default company name if none exists
}
// Check if the company exists in LACRM
$existing_company = $this->check_if_company_exists( $company_name );
// If the company doesn't exist, create it as a contact with IsCompany = true
if ( ! $existing_company ) {
$existing_company = $this->create_company( $company_name ); // Create the new company
}
// Check if a valid ContactId is available for the company
if ( $existing_company && isset( $existing_company['ContactId'] ) ) {
$company_id = $existing_company['ContactId']; // Use the ContactId for company association
} else {
error_log("No valid ContactId found for the company: $company_name");
$company_id = null; // Set company_id to null if no ContactId is found
}
// Prepare the buying group and ref
$buying_group = ["Davora Marketing"]; // Default group
$buying_group_ref = null;
// Check if the user is a Cardgains member
$cardgains_ref = get_user_meta( $user_id, 'afreg_additional_19125', true );
if ( ! empty( $cardgains_ref ) ) {
$buying_group[] = "Cardgains"; // Add "Cardgains" group
$buying_group_ref = $cardgains_ref; // Set the Buying Group Ref
}
// Prepare address
$address = [
"Street" => get_user_meta( $user_id, 'billing_address_1', true ),
"City" => get_user_meta( $user_id, 'billing_city', true ),
"State" => get_user_meta( $user_id, 'billing_state', true ),
"Zip" => get_user_meta( $user_id, 'billing_postcode', true ),
"Country" => get_user_meta( $user_id, 'billing_country', true )
];
// Return the parameters for creating/updating the contact
return [
"IsCompany" => false, // User is a contact, not a company
"AssignedTo" => 555885,
"Name" => $user->first_name . ' ' . $user->last_name,
"Email" => [
[ "Text" => $user->user_email, "Type" => "Work" ]
],
"Phone" => [
[ "Text" => get_user_meta( $user_id, 'billing_phone', true ), "Type" => "Work" ]
],
"Company Name" => $company_name, // Associate with the found or newly created company
"Accounts Ref" => get_user_meta( $user_id, 'sageref', true ),
"Address" => [$address],
"Buying Group" => $buying_group, // Add to one or more buying groups
"Buying Group Ref" => $buying_group_ref // Add the Cardgains membership ID, if applicable
];
}
// Create a contact in LACRM
public function create_contact( $user_id ) {
if ( get_user_meta( $user_id, 'afreg_new_user_status', true ) === 'approved' ) {
$parameters = $this->prepare_lacrm_parameters( $user_id );
$response = $this->call_lacrm_api( "CreateContact", $parameters );
if ( $response ) {
error_log( "LACRM API Response: " . print_r( $response, true ) );
} else {
error_log( "LACRM API: Failed to create contact." );
}
}
}
}