I'm looking for help on adjusting a priceset amount in a hook at the end of a membership signup. Until now under 4.7 for reasons of multi-level priceset complexity I've been adjusting priceset amounts via the hook_civicrm_validateForm by adjusting the values in the $form object. This has been working fine, but this is now longing working in 5.1.2. I realise that strictly speaking I shouldn't modify the $form in the validateForm hook. "The hook is intended for validation rather than altering form values." However I can't see anywhere else to change the amounts. I have two questions, i) Can anyone explain the difference in behaivour between 4.7 and 5.1.2? ii) is there a better place or method to adjust the priceset amount?
-
Did you ever try the buildAmount hook?– user4278Commented Jun 21, 2018 at 7:27
-
Hi, if I'm not mistaken buildAmount is called even before buildForm. I need to adjust the priceset amount taking into account fields adjusted by the user in the form interface. So I need to intervine at the time of submitting the form.– ArtiemartCommented Jun 21, 2018 at 7:58
Add a comment
|
1 Answer
You can use Build Amount hook to alter the price. If you want to trigger the change on form submit than you can use $form->_flagSubmitted. This is set to true when form is submitted. You can also use buildform or post process hook. But validateForm is a bad option.
function xxx_civicrm_buildAmount($pageType, &$form, &$amount) {
if (get_class($form) == 'CRM_Event_Form_Registration_Register' && $form->_flagSubmitted) {
foreach ($amount as $key =$value) {
foreach ($value['options'] as $pid =$value) {
$amount[$key]['options'][$pid]['amount'] = $value['amount'] - ($value['amount'] * 0.1);
}
}
}
}
HTH
Pradeep
-
Hi Pradeep, Interesting, I've now got the code executing in buildAmount as you suggested with the _flagSubmitted = true. The $form is being updated but the changes still aren't being transferred to the database. Here's the $form record I'm trying to update which worked before. $form->_priceSet['fields'][$priceset_id]['options'][$pricefield_id]['amount'] = $amount; R Commented Jun 21, 2018 at 10:50
-
use $amount variable to update the price.(updated ans to add smaple code snippet) Commented Jun 21, 2018 at 11:13
-
Thanks I've put it into place... $amount is correctly updated, but the values still aren't going into the database... zero in civicrm_financial_item. Commented Jun 21, 2018 at 12:10
-
-
<code> function hooks_civicrm_buildAmount( $pageType, &$form, &$amount ){ if($form->_flagSubmitted){ $priceset_fed = $form->_submitValues['priceset_fed']; $pricefield_fed = $form->_submitValues['pricefield_fed']; $priceset_club = $form->_submitValues['priceset_club']; $pricefield_club = $form->_submitValues['pricefield_club']; $amount[$priceset_fed]['options'][$pricefield_fed]['amount'] = $form->_submitValues['montant_quotepart_fed']; $amount[$priceset_club]['options'][$pricefield_club]['amount'] = $form->_submitValues['montant_quotepart_club']; } }</code> Commented Jun 21, 2018 at 12:27