Answers.Body: <p>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</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-01T11:02:52.560Z
Answers.LastActivityDate: 2018-09-01T11:02:52.560Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p><a href="https://i.sstatic.net/WEzIg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WEzIg.png" alt="enter image description here"></a>
  My Requirement : Whenever a membership is assigned to Head of Household , I need to assign memberships to Household Member and Household.</p>
  
  <p>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.</p>
  
  <p>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 </p>
  
  <blockquote>
    <p>Family <br>  Family (by relationship)<br></p>
  </blockquote>
  
  <p>For Household it creates 2 membership</p>
  
  <blockquote>
    <p>Family<br>  Family<br></p>
  </blockquote>
  
  <p>For Household Member it creates 1 membership</p>
  
  <blockquote>
    <p>Family (by relationship)<br></p>
  </blockquote>
  
  <p>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.</p>
  
  <p>My Code on Profile creation on hook_civicrm_post is</p>
  
  <pre><code>       add_filter( 'civicrm_post', 'alter_profiles_event', 10, 4  );
      function alter_profiles_event($op, $objectName, $objectId, &amp;$objectRef){
          if (!is_user_logged_in()){
      if ($objectName == 'Profile'  &amp;&amp; ($op == 'edit' || $op =='create') &amp;&amp; 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' =&gt; 1,
                  'return' =&gt; array(
                    "custom_19","first_name","last_name","contact_id"
                  ),
                  'id' =&gt; $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' =&gt; 'Individual',
                  'first_name' =&gt;  $objectRef['custom_19'],
                  'last_name'  =&gt; $objectRef['last_name'],
                // 'email' =&gt; $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' =&gt; 'Household',
                  'household_name' =&gt;  $objectRef['last_name'],
                  'email' =&gt; $objectRef['email-Primary'],
                );          
                $result2 = civicrm_api3('Contact', 'create', $params);
                }
  
                $household_id = $result2['id'];
                //create household relationship
                $head_of_household = array(
                  'contact_id_a' =&gt; $primary_contact_id, 
                  'contact_id_b' =&gt; $household_id,
                  'relationship_type_id' =&gt; 7,
                );
                $result3 = civicrm_api3('Relationship','create',$head_of_household);
                $household_member = array(
                  'contact_id_a' =&gt; $partner_contact_id,
                  'contact_id_b' =&gt; $household_id,
                  'relationship_type_id' =&gt; 8,
                );
                $result4 = civicrm_api3('Relationship','create',$household_member); 
  
                  $result5 = civicrm_api3('Address', 'get', array(
                    'contact_id' =&gt; $primary_contact_id,
                    'location_type_id' =&gt; 1,//"Main",
                    )
                  );      
                  if (!$result5['is_error']){
                  $id = $result5['id'];
                  $address = $result5['values'][$id];
                  $result5 = civicrm_api3('Address', 'create', array(
                    'contact_id' =&gt; $partner_contact_id,
                    'location_type_id' =&gt; "Main",//$address['location_type_id'],
                    'street_address' =&gt;   $address['street_address'],//Street Address
                    'supplemental_address_1' =&gt;   $address['supplemental_address_1'],//Supplemental Address
                    'city' =&gt;     $address['city'],//City
                    'state_province_id' =&gt;    $address['state_province_id'],//State
                    'country_id' =&gt;   $address['country_id'],//Country
                    )
                  );    
                  $result5 = civicrm_api3('Address', 'create', array(
                    'contact_id' =&gt; $household_id,
                    'location_type_id' =&gt; "Main",// $address['location_type_id'],
                    'street_address' =&gt;   $address['street_address'],//Street Address
                    'supplemental_address_1' =&gt;   $address['supplemental_address_1'],//Supplemental Address
                    'city' =&gt;     $address['city'],//City
                    'state_province_id' =&gt;    $address['state_province_id'],//State
                    'country_id' =&gt;   $address['country_id'],//Country
                    )
                  );      
                  }
              }
                  if ($objectName == 'Membership' &amp;&amp; ($op == 'create')){
                      print_r($op);
                      print_r($objectName);
                      print_r($objectRef);
                      die('ed');
              if ($objectRef-&gt;membership_type_id == 2){
                  $contact_id = $objectRef-&gt;contact_id;
                  $result = civicrm_api3('Contact', 'get', [
                   'sequential' =&gt; 1,
                   'contact_id' =&gt; $contact_id,
                  ]);
                  if ($result['values'][0]['contact_type'] == 'Individual'){
                    $result = civicrm_api3('Relationship', 'get', [
                      'sequential' =&gt; 1,
                      'relationship_type_id' =&gt; 7,
                      'contact_id_a' =&gt; $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' =&gt; 2,//"family",//@todo check if that works
        'contact_id' =&gt; $contact_id,
      ]);
        if (!isset($result['values'][0]['id'])){
          $result = civicrm_api3('Membership', 'create', [
            'membership_type_id' =&gt; 2,//"family",//@todo check if that works
            'contact_id' =&gt; $contact_id,
          ]);
        }
        else {
          if($result['values'][0]['end_date'] != $objectRef-&gt;end_date){
              $result = civicrm_api3('Membership', 'create', [
            'membership_type_id' =&gt; 2,//"family",//@todo check if that works
            'contact_id' =&gt; $contact_id,
          ]);
          }
        }
  }
  </code></pre>
  
  <p>When I don't add code to create membership for a household programmatically, it does not create membership for
  Household member and Household both</p>
  
Tags: <wordpress><civimember><relationships>
Title: Creating a Membership programmatically for Household creates multiple memberships?
ThreadText: # Creating a Membership programmatically for Household creates multiple memberships?
  
  (Asked by: username_0 on 2018-09-01)
  
  [![enter image description here](https://i.sstatic.net/WEzIg.png)](https://i.sstatic.net/WEzIg.png)
  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
  
  **Tags:** wordpress, civimember, relationships
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-01. Score: 1 | Accepted answer
  
  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
  
  
  ---
  
  