Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created May 21, 2024 20:23
Show Gist options
  • Save Lonsdale201/53320d27ff5ecfe1de3607411ea8dd95 to your computer and use it in GitHub Desktop.
Save Lonsdale201/53320d27ff5ecfe1de3607411ea8dd95 to your computer and use it in GitHub Desktop.
JetEngine Custom macro - Fluent CRM get Contact users with selectable contact type
// place the code in the child theme functions.php or a custom code snippets plugin like FluentSnippets
// how to use
// Create a new Users Query in the Query builder
// Go to the Include/Exclude section
// click the Include section dynamic icon and choose the Fluent CRM Users with Contact Type
// HELP IMAGE https://prnt.sc/06FFNY9E24Sg
// This macro returns the identifiers of users who can be found in fluentCRM as both contacts and wordpress users.
// In addition, you can select which Contact type you want to retrieve users from.
add_action( 'jet-engine/register-macros', function() {
class Fluent_CRM_Users_With_Contact_Type extends \Jet_Engine_Base_Macros {
public function macros_tag() {
return 'fluent_crm_users_with_contact_type';
}
public function macros_name() {
return 'Fluent CRM Users with Contact Type';
}
public function macros_args() {
$contact_types = fluentcrm_contact_types(true);
$type_options = array();
foreach ($contact_types as $type) {
$type_options[$type['id']] = $type['title'];
}
return array(
'contact_type' => array(
'label' => 'Contact Type',
'type' => 'select',
'options' => $type_options,
'default' => key($type_options)
),
);
}
public function macros_callback( $args = array() ) {
$contact_type = !empty( $args['contact_type'] ) ? $args['contact_type'] : key($args['contact_type']);
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$crm_users = array();
$contactApi = FluentCrmApi('contacts')->getInstance();
$contacts = $contactApi->where('contact_type', $contact_type)->get();
foreach ( $users as $user ) {
foreach ( $contacts as $contact ) {
if ( $contact->user_id == $user->ID ) {
$crm_users[] = $user->ID;
break;
}
}
}
if ( empty( $crm_users ) ) {
return 'No CRM users found';
}
return implode( ',', $crm_users );
}
}
new Fluent_CRM_Users_With_Contact_Type();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment