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) »
  • Need to modify price based on how many contacts are selected
Pages: [1]

Author Topic: Need to modify price based on how many contacts are selected  (Read 1094 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Need to modify price based on how many contacts are selected
February 11, 2011, 09:33:04 am
I am planning to modify a contribution page profile so that it displays a list of contacts with a checkbox next to each contact's name.

I am planning to use the hook hook_civicrm_buildForm( $formName, &$form ) as the spot to add my code for getting a group of contacts to display.

The price for each person is $2.  So if the donor puts a checkmark next to 25 people's names, they need to pay $50.

To modify the donation amount based on how many people's names are chosen, I am planning to implement the hook "hook_civicrm_buildAmount( $pageType, &$form, &$amount )"

Is this a good approach to take for this?  Is the buildAmount called after someone makes their choices of which names they want?

( FYI: The purpose of this is to allow each person to donate $2. to send a holiday greeting to other people in their community )
 



Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Need to modify price based on how many contacts are selected
February 14, 2011, 11:38:26 am
Is this for a membership form? Otherwise can't you just use a price set?

NB - there is a module out there for pricesets with memberships although it has some limitations
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Need to modify price based on how many contacts are selected
February 14, 2011, 04:16:13 pm
I need to present a list of all the people that they could send a card to.  Then people put a check box next to the intended recipient of the card.      Most people only send cards to to their friends and family, not to everyone in the community.

( Its actually a nice fundraiser. People normally send holiday cards to friends and family anyhow, and for a donation of a few dollars, the nonprofit sends it for you.  Plus if one person "receives" 10 cards, the nonprofit only gives them one card with the 10 names of the various senders.  )
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Need to modify price based on how many contacts are selected
February 14, 2011, 06:19:34 pm
Yeah - that's pretty groovey!

I think the build amount affects 'normal' prices & build_form does price sets .... but it might be vice versa. You want to build up a price set 'on the fly' really don't you? I think if you created the price field & then just added people as options in your hook
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Need to modify price based on how many contacts are selected
February 17, 2011, 06:55:05 am
I was able to use the "buildAmount" hook to get the extra choices into the price set.  I defined the priceset field as a checkbox then in the hook I add extra options.    It correctly calculates the price when the donor chooses various people to get the note.  This part is working fine.

My new checkbox option is called "family_4"

BUT, when I try to complete the donation, on the page after the confirmation screen I get the error message:

Database Error Code: Cannot add or update a child row: a foreign key constraint fails (`pogstone_drpl1`.`civicrm_line_item`, CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (`price_field_value_id`) REFERENCES `civicrm_price_field_value` (`id`) ON DELETE SET NULL), 1452
Additional Details:
Array
(
    [callback] => Array
        (
           
  • => CRM_Core_Error
  • [1] => handle
            )

       
Code: [Select]
=> -3
    [message] => DB Error: constraint violation
    [mode] => 16
    [debug_info] => INSERT INTO civicrm_line_item (entity_table , entity_id , price_field_id , label , qty , unit_price , line_total , participant_count , price_field_value_id ) VALUES ('civicrm_contribution' ,  7 ,  1 , 'family 4' ,  1 ,  2.50 ,  2.5 ,  0 ,  4 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`pogstone_drpl1`.`civicrm_line_item`, CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (`price_field_value_id`) REFERENCES `civicrm_price_field_value` (`id`) ON DELETE SET NULL)]
    [type] => DB_Error
    [user_info] => INSERT INTO civicrm_line_item (entity_table , entity_id , price_field_id , label , qty , unit_price , line_total , participant_count , price_field_value_id ) VALUES ('civicrm_contribution' ,  7 ,  1 , 'family 4' ,  1 ,  2.50 ,  2.5 ,  0 ,  4 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`pogstone_drpl1`.`civicrm_line_item`, CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (`price_field_value_id`) REFERENCES `civicrm_price_field_value` (`id`) ON DELETE SET NULL)]
    [to_string] => [db_error: message="DB Error: constraint violation" code=-3 mode=callback callback=CRM_Core_Error::handle prefix="" info="INSERT INTO civicrm_line_item (entity_table , entity_id , price_field_id , label , qty , unit_price , line_total , participant_count , price_field_value_id ) VALUES ('civicrm_contribution' ,  7 ,  1 , 'family 4' ,  1 ,  2.50 ,  2.5 ,  0 ,  4 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`pogstone_drpl1`.`civicrm_line_item`, CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (`price_field_value_id`) REFERENCES `civicrm_price_field_value` (`id`) ON DELETE SET NULL)]"]
)
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Need to modify price based on how many contacts are selected
February 17, 2011, 07:04:18 am
If the donor only chooses one of the static, predefined checkboxes, then the page works fine. It only breaks when the donor chooses a option that my hook added.
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Rajan Mayekar

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 20
    • Rajan's Blogs
Re: Need to modify price based on how many contacts are selected
February 17, 2011, 09:14:08 am
Quote
It only breaks when the donor chooses a option that my hook added
Because it is not getting valid price field value Id, ie the field which is you are building through the hook is not present in db.
You can do vise-versa like instead of adding field through hook when it is needed,  you can unset the field when it is not needed ( or change just field amount ). It will definitely solve this issue.

Rajan

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Need to modify price based on how many contacts are selected

This forum was archived on 2017-11-26.