0

enter image description here My Requirement : Whenever a membership is assigned to Head of Household , I need to assign memberships to Household Member and Household.

I have a membership type Family with "Head of household, household member" as related contact to be assigned with the membership in the settings of membership.

Whenever the Head of Household fills the membership form, It creates a member of Household and a Household. Also, Membership assigned to Head of Household is assigned to Household programmatically as seen in the code below I am unable to understand why there are multiple memberships generated For Head of Household, It creates 2 Memberships

Family
Family (by relationship)

For Household it creates 2 membership

Family
Family

For Household Member it creates 1 membership

Family (by relationship)

I need to know the reason of duplication and how I can avoid duplication and create memberships 1 each for Head of Household, Household member and Household.

My Code on Profile creation on hook_civicrm_post is

       add_filter( 'civicrm_post', 'alter_profiles_event', 10, 4  );
    function alter_profiles_event($op, $objectName, $objectId, &$objectRef){
        if (!is_user_logged_in()){
    if ($objectName == 'Profile'  && ($op == 'edit' || $op =='create') && isset($objectRef['custom_19'])){

          $primary_email = $objectRef['email-Primary'];
            $primary_contact_id = civicrm_family_profile_get_primary_contact_id($primary_email);

            $result = civicrm_api3('Contact', 'get', 
              array(
                'sequential' => 1,
                'return' => array(
                  "custom_19","first_name","last_name","contact_id"
                ),
                'id' => $primary_contact_id,
              )
            );  
            $is_household = FALSE;
            $last_name =  $result['values'][0]['last_name'];
            $household_id = '';
            $custom_19_first_name = $objectRef['custom_19'];
            $result1 = array();
            if (isset($objectRef['custom_19'])){
              $params = array(
                'contact_type' => 'Individual',
                'first_name' =>  $objectRef['custom_19'],
                'last_name'  => $objectRef['last_name'],
              // 'email' => $objectRefArr['custom_21'],
              );
                //partner contact created
              $result1 = civicrm_api3('Contact', 'create', $params);
            }
            $partner_contact_id  = $result1['id'];

              $result2 = array();
              //create household
            if (isset($objectRef['last_name'])){

              $params = array(
                'contact_type' => 'Household',
                'household_name' =>  $objectRef['last_name'],
                'email' => $objectRef['email-Primary'],
              );          
              $result2 = civicrm_api3('Contact', 'create', $params);
              }

              $household_id = $result2['id'];
              //create household relationship
              $head_of_household = array(
                'contact_id_a' => $primary_contact_id, 
                'contact_id_b' => $household_id,
                'relationship_type_id' => 7,
              );
              $result3 = civicrm_api3('Relationship','create',$head_of_household);
              $household_member = array(
                'contact_id_a' => $partner_contact_id,
                'contact_id_b' => $household_id,
                'relationship_type_id' => 8,
              );
              $result4 = civicrm_api3('Relationship','create',$household_member); 

                $result5 = civicrm_api3('Address', 'get', array(
                  'contact_id' => $primary_contact_id,
                  'location_type_id' => 1,//"Main",
                  )
                );      
                if (!$result5['is_error']){
                $id = $result5['id'];
                $address = $result5['values'][$id];
                $result5 = civicrm_api3('Address', 'create', array(
                  'contact_id' => $partner_contact_id,
                  'location_type_id' => "Main",//$address['location_type_id'],
                  'street_address' =>   $address['street_address'],//Street Address
                  'supplemental_address_1' =>   $address['supplemental_address_1'],//Supplemental Address
                  'city' =>     $address['city'],//City
                  'state_province_id' =>    $address['state_province_id'],//State
                  'country_id' =>   $address['country_id'],//Country
                  )
                );    
                $result5 = civicrm_api3('Address', 'create', array(
                  'contact_id' => $household_id,
                  'location_type_id' => "Main",// $address['location_type_id'],
                  'street_address' =>   $address['street_address'],//Street Address
                  'supplemental_address_1' =>   $address['supplemental_address_1'],//Supplemental Address
                  'city' =>     $address['city'],//City
                  'state_province_id' =>    $address['state_province_id'],//State
                  'country_id' =>   $address['country_id'],//Country
                  )
                );      
                }
            }
                if ($objectName == 'Membership' && ($op == 'create')){
                    print_r($op);
                    print_r($objectName);
                    print_r($objectRef);
                    die('ed');
            if ($objectRef->membership_type_id == 2){
                $contact_id = $objectRef->contact_id;
                $result = civicrm_api3('Contact', 'get', [
                 'sequential' => 1,
                 'contact_id' => $contact_id,
                ]);
                if ($result['values'][0]['contact_type'] == 'Individual'){
                  $result = civicrm_api3('Relationship', 'get', [
                    'sequential' => 1,
                    'relationship_type_id' => 7,
                    'contact_id_a' => $contact_id,
                  ]);
                  if(isset($result['values'][0])){
                    $contact_id_b = $result['values'][0]['contact_id_b'];//household
                    civicrm_family_profile_create_membership($contact_id_b,$objectRef);
                  }
                }
            }
          }
        }
    }

    function civicrm_family_profile_create_membership($contact_id,$objectRef){

      $result = civicrm_api3('Membership', 'get', [
      'membership_type_id' => 2,//"family",//@todo check if that works
      'contact_id' => $contact_id,
    ]);
      if (!isset($result['values'][0]['id'])){
        $result = civicrm_api3('Membership', 'create', [
          'membership_type_id' => 2,//"family",//@todo check if that works
          'contact_id' => $contact_id,
        ]);
      }
      else {
        if($result['values'][0]['end_date'] != $objectRef->end_date){
            $result = civicrm_api3('Membership', 'create', [
          'membership_type_id' => 2,//"family",//@todo check if that works
          'contact_id' => $contact_id,
        ]);
        }
      }
}

When I don't add code to create membership for a household programmatically, it does not create membership for Household member and Household both

2

Since you have configured membership type to use relationship it will automatically add membership to contact having relationship with the primary membership holder. So you have to exclude code in your plugin that creates memberships for other contacts as Civi already does that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.