2

I'm using CiviCRM 4.5.8 and am setting it up to handle a three year fundraising campaign for our church.

Our church almost exclusively records donations for Households and not for individuals.

How can I create a Contribution Page that allows a person to make Pledges and Contributions that will be linked to their Household?

I am not afraid of custom coding and am currently planning to write a script that automatically shifts all individual pledges to their related household, but if I do that, I still want the contribution page to work for that person to actually make contributions.

Ideally, I'd just use a template override to inject the contact_id of the user's household if there is one or to use the individual contact_id otherwise, but I need some help on how to do that.

I already am comfortable working with template overrides, but I'm asking if anyone knows which template needs to be overridden to get the contact_id in the correct place.

EDIT: SOLUTION CODE

/**
 * Implements hook_civicrm_post
 */
function householdcontributions_civicrm_post($op, $objectName, $objectId, &$objectRef) {
    // the _post hook is called AFTER the object has been written to the database
    // because of that, we need to change the contact_id to the household_contact_id
    // and save
    if($op == 'create' && $objectName == 'Contribution') {

        $current_contact_id = $objectRef->contact_id;
        $real_id = $current_contact_id;


        // look for a household associated with this contact
        $result = civicrm_api3('Relationship', 'get', array(
          'sequential' => 1,
          'relationship_type_id' => 7,
          'contact_id_a' => $current_contact_id
        ));


        // override the contact_id with the newly discovered household id
        if ($result['count'] == 1)
        {
            $household_id = $result['values'][0]['contact_id_b'];
            // $objectRef->contact_id = $household['values'][0]['contact_id_b'];
            $result = civicrm_api3('Contribution', 'create', array(
              'sequential' => 1,
              'id' => $objectId,
              'contact_id' => $household_id,
            ));
        }
    }
}

2 Answers 2

1

I don't know if a template override can do this, but an extension may be a good option. You'll need logic to filter on specific contribution pages or other business rules, but here's the basic idea of hooking into the post process action and moving the contribution from the current contact to the household. Another option to consider may be to automatically credit households for donor gifts so that you can give credit to the household but properly track the donor for tax purposes.

/**
 * Implementation of hook_civicrm_post
 */
function myextension_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if($op == 'create' && $objectName == 'Contribution') {

    $current_contact_id = $objectRef['contact_id'];

    $household_id = civicrm_api3('Relationship', 'get', array(
      'sequential' => 1,
      'relationship_type_id' => 8,
      'contact_id_a' => $current_contact_id 
    ));



    $result = civicrm_api3('Contribution', 'create', array(
      'sequential' => 1,
      'id' => $objectId,
      'contact_id' => $household_id['contact_id_b'],
    ));
  }
}
3
  • I added a CMS-agnostic way of getting the contact_id and avoided an endless loop when your hook_post triggers itself! This does NOT leave a way to give individually though, but that wouldn't be hard to add from here. Apr 18, 2015 at 3:16
  • Thanks for the fixes! I did a quick copy/paste from the developer wiki and then added the API calls, but didn't think about checking the $op. @Jeff, this just a starter; you'll need more business logic to prevent all gifts from going to households like Jon mentioned, only fire when a contact is part of a household, etc.
    – Nicholai
    Apr 18, 2015 at 12:47
  • I had to edit it somewhat... original post edited with my final code. May 9, 2015 at 3:00
2

I do something very similar with my church. the actual donation is credited to the individual who made it with a soft credit going to the household. This allows us to do reporting by either household or individual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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