CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changing the contribution_type_id of a contribution dynamically
Pages: [1]

Author Topic: Changing the contribution_type_id of a contribution dynamically  (Read 1696 times)

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Changing the contribution_type_id of a contribution dynamically
October 19, 2009, 09:09:40 pm
By default, CiviContribute links the donation type (and therefore contribution_type_id) by the contribution page.  This model is not working well for my current customer, who uses accounting codes, and has a lot of them.   Creating a new page for every code is not practical.

So I'm trying to figure out how to set contribution_type_id in a contribution.  I need information from the $form object to do this, but I'm having difficulty finding a place where CiviCRM will let me change the code AND give me enough context information to do this.

hook_civicrm_pre()  DOES have the ability to change the field, but it's unclear how I can get at the form data.  Supposedly the form gets stuffed into a singleton.  BUT HOW DO YOU GET BACK THE F***KING SINGLETON WHEN YOU ACTUALLY NEED IT?

I've tried a mess of things.  This is far more complex than it needs to be :-(

I've tried setting this stuff at hook_civicrm_postProcess time among others, but I don't see any place that has enough information:

I need:

  • Either the contribution_id or access to the data that will actually be used to construct the contribution if it does not yet exist
  • Form context from the contribution page.

What's a process to do this that does not involve munging POST parameters (a serious security risk) or doing undocumented rooting through SESSION objects?

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: Changing the contribution_type_id of a contribution dynamically
October 19, 2009, 10:57:00 pm
Quote
So I'm trying to figure out how to set contribution_type_id in a contribution.

A easier solution to change/ over-ride the contribution type for a contribution done through contribution page is to create a profile having field "Contribution Type". By default, we suppress this field in the profile fields on a contribution page,
so you might want to apply the following patch in order to have the field in your profile :

Code: [Select]
Index: CRM/Contribute/Form/ContributionBase.php
===================================================================
--- CRM/Contribute/Form/ContributionBase.php (revision 24260)
+++ CRM/Contribute/Form/ContributionBase.php (working copy)
@@ -661,7 +661,6 @@
                                      'contribution_status_id' => 1,
                                      'payment_instrument'     => 1,
                                      'check_number'           => 1,
-                                     'contribution_type'      => 1
                                      );
 
             $fields = null;

HTH
-Yashodha
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Changing the contribution_type_id of a contribution dynamically
October 22, 2009, 11:07:49 pm
I'll admit that this is not too appealing a suggestion, since I'd much rather add a field or two to a contribution template than attempt to play with the profile rendering code.  That sounds like a cure worse than the disease  ::)

I'm trying out a suggestion of Lobo's to write the contribution back out at hook_civicrm_postProcess time, although I think I've encountered a bug in civicrm_contribute_add as well -- it appears that it is not allowing incremental updates of a contribution, and will not allow you to update a contribution and still preserve both the transaction id and the contribution id.

alphageekboy

  • I’m new here
  • *
  • Posts: 11
  • Karma: 1
  • Web Developer
    • Alpha Geek Tech, LLC
Re: Changing the contribution_type_id of a contribution dynamically
October 30, 2009, 11:19:14 am
I too am looking for something similar.  I want to use on contribution_type_id for the Membership Dues/Signup, and a different one for the Additional Contribution.  When I post my data back to quickbooks, I need to post to two different accounts depending on if it was a "donation" or if it was a "membership due".  I started looking and trying things in hook_civicrm_postProcess, but I don't think I'm familiar enough with the CiviCRM code/Smarty implementation to get any decent results.  I see some of the fields I need, but not all of them.  I've looked at examples of the postProcess hook, but there is still a learning curve and feel like I'm just missing something.

In Summary, I want to hook the process either before it writes the contribution or update the contribution based on the following:

1. If it is a membership due amount then write contribution normally.
2. If it is an "amount_other" (contribution/donation), then change the contribution_type_id either before it writes for the contribution, or update the particular contribution with a new id.
Tracy C. Smith
Chief Technologist & Founder

Alpha Geek  Tech, LLC
p: 831.706.2187
http://alphageektech.net
skype/aol: tracycsmith
t: alphageektech

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Changing the contribution_type_id of a contribution dynamically
October 30, 2009, 11:50:35 am

i think the easiest way will be to:

1. implement hook_civicrm_postProcess

2. update the contribution records AFTER civicrm has written them to the DB

the hard part will be figuring out what are the contribution ids and whether is it a membership record or not. Hopefully there will be enough info in the forms for you to extract that

ping us on IRC if u need help

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changing the contribution_type_id of a contribution dynamically

This forum was archived on 2017-11-26.