Skip to content
Snippets Groups Projects
Commit a1fefaf4 authored by Sean Madsen's avatar Sean Madsen Committed by GitHub
Browse files

Merge pull request #385 from mattwire/patch-15

Add a new working example for hook_civicrm_buildAmount
parents ef15b780 d4cbe301
No related branches found
No related tags found
No related merge requests found
...@@ -22,68 +22,51 @@ registration fees. ...@@ -22,68 +22,51 @@ registration fees.
- null - null
## Example ## Example
For an extension that implements this hook see: https://github.com/mattwire/uk.org.som.proratamembership
function civitest_civicrm_buildAmount( function proratamembership_civicrm_buildAmount($pageType, &$form, &$amount) {
$pageType, if (!empty($form->get('mid'))) {
&$form, // Don't apply pro-rated fees to renewals
&$amount return;
) { }
//sample to modify priceset fee //sample to modify priceset fee
$priceSetId = $form->get( 'priceSetId' ); $priceSetId = $form->get('priceSetId');
if ( !empty( $priceSetId ) ) { if (!empty($priceSetId)) {
$feeBlock =& $amount; $feeBlock = &$amount;
// if you use this in sample data, u'll see changes in if (!is_array($feeBlock) || empty($feeBlock)) {
// contrib page id = 1, event page id = 1 and
// contrib page id = 2 (which is a membership page
if (!is_array( $feeBlock ) || empty( $feeBlock ) ) {
return;
}
//in case of event we get eventId,
//so lets apply hook for eventId = 1
if ( $pageType == 'event' && $form->_eventId != 1 ) {
return; return;
} }
//in case of contrbution
//for online case we get page id so we could apply for specific
if ( $pageType == 'contribution' ) {
if ( !in_array(
get_class( $form ),
array( 'CRM_Contribute_Form_Contribution', 'CRM_Contribute_Form_Contribution_Main')
)
) {
return;
}
}
if ($pageType == 'membership') { if ($pageType == 'membership') {
// give a discount of 20% to everyone // pro-rata membership per month
foreach ( $feeBlock as &$fee ) { // membership year is from 1st Jan->31st Dec
if ( !is_array( $fee['options'] ) ) { // Subtract 1/12 per month so in Jan you pay full amount,
// in Dec you pay 1/12
// 12 months in year, min 1 month so subtract current numeric month from 13 (gives 12 in Jan, 1 in December)
$monthNum = date('n');
$monthsToPay = 13-$monthNum;
foreach ($feeBlock as &$fee) {
if (!is_array($fee['options'])) {
continue; continue;
} }
foreach ( $fee['options'] as &$option ) { foreach ($fee['options'] as &$option) {
//for sample lets modify first option from all fields. // We only have one amount for each membership, so this code may be overkill,
$option['amount'] = $option['amount'] * 0.8; // as it checks every option displayed (and there is only one).
$option['label'] .= ' - ' . ts( 'Get a 20% discount since you know this hook' ); if ($option['amount'] > 0) {
} // Only pro-rata paid memberships!
} $option['amount'] = $option['amount'] * ($monthsToPay / 12);
} else { if ($monthsToPay == 1) {
// unconditionally modify the first option to be a $100 fee $option['label'] .= ' - Pro-rata: Dec only';
// to show our power! }
foreach ( $feeBlock as &$fee ) { elseif ($monthsToPay < 12) {
if ( !is_array( $fee['options'] ) ) { $dateObj = DateTime::createFromFormat('!m', $monthNum);
continue; $monthName = $dateObj->format('M');
} $option['label'] .= ' - Pro-rata: ' . $monthName . ' to Dec';
foreach ( $fee['options'] as &$option ) { }
//for sample lets modify first option from all fields. }
$option['amount'] = 100;
$option['label'] = ts( 'Power of hooks' );
break;
} }
} }
// FIXME: Somewhere between 4.7.15 and 4.7.23 the above stopped working and we have to do the following to make the confirm page show the correct amount.
$form->_priceSet['fields'] = $feeBlock;
} }
} }
} }
It may be me... but I'm pretty sure that this documentation needs to be
updated because since 4.2 everything is in a price set... This code
doesn't actually work to my knowledge.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment