• Resolved fullworks

    (@fullworks)


    I’m hoping I can make use of this plugin to save time coding some bespoke solution.

    On our CiviCRM we have Memberships at ‘Household’ level and each Houshold has an Individual related as Head of Household.

    The email addresses sit with the Head of Household ( GDPR issue as far as I understand – as houses don’t move but people do )

    What I’d like to do is generate a WP User per Head of Household for each Household that have a valid membership.

    I have been look through the code but not yet worked out a solution.

    One thought is when creating the WP User object that there is a simple look up of the Head of Household to get the email.

    Any thoughts?

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Christian Wach

    (@needle)

    @fullworks I don’t think there’s anything preventing the Household from being synced to a WordPress User as it is. There’s a “Synchronize Individuals only” option on the CWMS settings page – have you unchecked that and tried what you want to achieve (on a dev site of course)?

    Plugin Author Christian Wach

    (@needle)

    @fullworks Ah, hmm, I think I misread your question:

    One thought is when creating the WP User object that there is a simple look up of the Head of Household to get the email.

    Yes, that seems sensible. Filters/actions I’d look at are:

    * civi_wp_member_sync_auto_create_wp_user to prevent Individuals and Organizations from being synced
    * civi_wp_member_sync_before_insert_user to inspect the Contact prior to creating a WordPress User

    However it looks like you want to amend the Contact data before the User is created… for which there isn’t a filter. If that’s the case, I can add one.

    There may also be a CiviCRM hook that allows the data returned from an API call to be amended before being returned – hook_civicrm_apiWrappers looks like a good candidate – so you could consider amending all Household queries to include the email address of the Head of Household.

    Thread Starter fullworks

    (@fullworks)

    OK I have nearly a solution in public function civi_get_contact_by_contact_id

    which returns the CiviCrm Contact data

    I look up the relationship and switch the contact data

    It nearly works perfectly except Roles – interestingly the ‘expired’ role is assigned correctly but the ‘current’ role ends up as none

    public function civi_get_contact_by_contact_id( $contact_id ) {
    
    		// Kick out if no CiviCRM.
    		if ( ! civi_wp()->initialize() ) return false;
    
    		// Bail if we don't get a valid contact ID.
    		if ( empty( $contact_id ) OR ! is_numeric( $contact_id ) ) return false;
    
    		// Get all contact data.
    		$params = array(
    			'version' => 3,
    			'contact_id' => $contact_id,
    		);
    
    		// Use API.
    		$contact_data = civicrm_api( 'contact', 'get', $params );
    		
    		// Find the Head of Household contact ID
    
    		$relationship = civicrm_api3( 'Relationship', 'get', array(
    			'sequential' => 1,
    			'return' => ["contact_id_a"],
    			'contact_id_b' => $contact_id,
    			'relationship_type_id' => 7,
    			'is_active' => 1,
    		));
    		
    		// Now switch the contact data.
    		
    		$params = array(
    			'version' => 3,
    			'contact_id' => $relationship['values'][0]['contact_id_a'],
    		);
    
    		// Use API.
    		$contact_data = civicrm_api( 'contact', 'get', $params );
    
    		// Bail if we get any errors.
    Thread Starter fullworks

    (@fullworks)

    This hook_civicrm_apiWrappers may be the right way, and whilst I have written CivCRM extension I’m a WordPress plugin developer and switching between the two concepts I find really hard ??

    Plugin Author Christian Wach

    (@needle)

    @fullworks No need for an extension: see how this plugin hooks into CiviCRM as an example.

    I see where you’re going with your code – but it’s not wise to alter the plugin code directly. That’s why I pointed you to the filter and action. As I said, I’ll add a filter so that the Contact data can be amended before the WordPress User is created.

    Plugin Author Christian Wach

    (@needle)

    @fullworks I’ve added the filter to the development repo:

    https://github.com/christianwach/civicrm-wp-member-sync/blob/master/includes/civi-wp-ms-users.php#L585-L593

    Use that to filter the Contact data instead of directly altering the plugin.

    Cheers, Christian

    Thread Starter fullworks

    (@fullworks)

    Thanks,

    I was going to do a pull request with a filter in place once I worked out if the strategy works you saved me the task.

    Alan

    Plugin Author Christian Wach

    (@needle)

    @fullworks It’ll be present in the next release, but it sounds like you know what you’re doing. If you share your code here, then there’s a trail left for others who might want to do something similar.

    Cheers, Christian

    Thread Starter fullworks

    (@fullworks)

    Plugin Author Christian Wach

    (@needle)

    @fullworks Looks okay to me.

    Thread Starter fullworks

    (@fullworks)

    Thanks, it works for at least manual bulk creation.

    Wasn’t 100% sure if you used display_name or sort_name in generating the user names, so I was lazy and did both rather than reading the code.

    Also, not sure of what happens yet ( i.e. haven’t tested ) if I change the email on the related member in CiviCRM.

    Plugin Author Christian Wach

    (@needle)

    @fullworks That would need separate handling. Look at the code in CiviCRM WordPress Profile Sync to see how you can keep a Contact’s details in sync with a WordPress User’s details.

    Thread Starter fullworks

    (@fullworks)

    Thanks,

    Strange that a relatively simple job always ends up being so much more ??

    While I have your expertise on tap – the ultimate goal here is to have the hardcopy magazine available for paid up members.

    So obviously members sync will allow me to have roles that can be used for protection rules.

    In terms of protection ( i.e. only seeing the magazine & archive when logged ) I was planning to custom code, as it isn’t that complex – but that said there are many plugins that do similar – any that you would use?

    Plugin Author Christian Wach

    (@needle)

    While I have your expertise on tap

    @fullworks FWIW, the plugins I write are free, not the support or professional advice! But anyway… I’d use Groups for this.

    Thread Starter fullworks

    (@fullworks)

    Thanks and understood.

    Does the donate button go to you directly?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Using a Related Individual to create the user’ is closed to new replies.