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 (Moderator: Donald Lobo) »
  • Buildamount hook clarfication
Pages: [1]

Author Topic: Buildamount hook clarfication  (Read 1820 times)

jeevajoy

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4
  • CMS version: joomal 1.6
  • MySQL version: 5
  • PHP version: 5
Buildamount hook clarfication
January 03, 2012, 03:16:36 am
Hi,

I'm using buildmaount hook for event to give membership discount. I'm getting the email address using jquery and checking membership by passing the email id in url and using $_GET method retrieving email. Then using Api to check membership of the email id.
Its working in the register page. it gives the discounted amount. But when i go to the Confirm page it gives Original amount and not the discounted amount.

Any idea.

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Buildamount hook clarfication
January 03, 2012, 05:41:48 am
I would suggest checking buildAmount hook implementation at http://svn.civicrm.org/civicrm/branches/v4.1/drupal/civitest.module.sample

Hth

Kurund
Found this reply helpful? Support CiviCRM

probate

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.07
  • CMS version: Drupal 7.8
  • MySQL version: 5.1.54
  • PHP version: 5.3.6
Re: Buildamount hook clarfication
February 28, 2012, 05:40:30 pm
I have been trying for hours to get the buildAmount hook to change the values on a price set for an event registration, but nothing I do makes any difference.

I can update the ['amount'] element and have the changed value displayed in a watchdog message, but there is no change to the value displayed when the event registration page is loaded.

I am also calling the buildForm hook. Could this be overwriting changes?

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Buildamount hook clarfication
June 13, 2012, 08:45:54 am
@probate -- did you get this working? i am having the same issue and can't seem to get it to display on my confirmation page. any help appreciated.

Jag

  • I’m new here
  • *
  • Posts: 12
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7.8
  • MySQL version: 5.5.8
  • PHP version: 5.3.5
Re: Buildamount hook clarfication
June 14, 2012, 04:23:52 am
Can you tell me how you are checking the form in buildAmount hook

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Buildamount hook clarfication
June 14, 2012, 04:37:46 am
here is my buildAmount function. it all works fine on the event registration form, but once it goes to the confirmation screen, it will not work where it runs the part of $code = _get_manual_extra_codes() that allows me to pull information from a webform that's been submitted.

it seems to me that when it runs that, the discountcode element does not stay set, but it stays set otherwise. so i was then trying to set that form element based on if the GET variable is there (as it's available on the registration screen but not on the confirmation screen).

how can i make it so that it passes over?

 
Code: [Select]
/**
 * Implementation of hook_civicrm_buildAmount()
 *
 * If the event id of the form being loaded has a discount code, calculate the
 * the discount and update the price and label. Apply the initial autodiscount
 * based on a users membership.
 *
 * Check all priceset items and only apply the discount to the discounted items.
 */
function civievent_discount_civicrm_buildAmount($pagetype, &$form, &$amounts) {
  if ($pagetype == 'event') {

    $v = $form->getVar('_values');
    $currency = $v['event']['currency'];

    /**
     * If additional participants are not allowed to receive a discount we need
     * to interrupt the form processing on build and POST.
     *
     * This is a potential landmine if the form processing ever changes in Civi.
     */
    if (!_allow_multiple()) {
      // POST from participant form to confirm page
      if ($form->getVar('_lastParticipant') == 1) {
        if (!_allow_multiple()) { return; }
      }

      // On build participant form
      $keys = array_keys($_GET);
      foreach ($keys as $key) {
        if (substr($key, 0, 16) == "_qf_Participant_") {
          // We can somewhat safely assume we're in the additional participant registration screen.
          if ($_GET[$key] == 'true') { return; }
        }
      }
    }

    require_once('civievent_discount.admin.inc');
    $codes = _get_discounts();
    $code = CRM_Utils_Request::retrieve('discountcode', 'String', $form, false, null, $_REQUEST);
   
    if (!$code) { $code = _verify_autodiscount($codes); }
    if (!$code && $_GET['sid']) {
      $code = _get_manual_extra_codes();

      //$form->_values['discountcode'] = $code;
      //$form->getVar( 'discountcode' );
      //$form->setVar( 'discountcode', $code );
    }
   
    $code = _get_code_details($code);
    if (!$code) { return; }
    if (_is_expired($code)) { return; }
    $eid = $form->getVar('_eventId');
    $psid = $form->get('priceSetId');
    $eids = _get_discounted_event_ids();

    if (!empty($psid)) {
      $feeblock =& $amounts;
      $psids = _get_discounted_priceset_ids();
      if (!in_array($pagetype, array('contribution', 'event')) || !is_array($feeblock) || empty($feeblock)) { return; }
      if ($pagetype == 'event') {
        if (!in_array($eid, $eids) && !in_array($psid, $psids)) { return; }
      }
      if ($pagetype == 'contribution') {
        if (!in_array(get_class($form), array('CRM_Contribute_Form_Contribution', 'CRM_Contribute_Form_Contribution_Main'))) {
          return;
        }
      }
      foreach ($feeblock as &$fee) {
        if (!is_array($fee['options'])) { continue; }
        foreach ($fee['options'] as &$option) {
          if (in_array($option['id'], $psids)) {
            if (in_array($option['id'], unserialize($code['pricesets']))) {
              list($option['amount'], $option['label']) =
                  _calc_discount($option['amount'], $option['label'], $code, $currency);
            }
          }
        }
      }
    } else {
      if (in_array($eid, unserialize($code['events']))) {
        foreach ($amounts as $aid => $vals) {
          list($amounts[$aid]['value'], $amounts[$aid]['label']) =
              _calc_discount($vals['value'], $vals['label'], $code, $currency);
             
        }
      }
    }
  } else { return; }
}

function _get_manual_extra_codes(){
    if(!$code) {
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$sid = $_GET['sid'];

// Load the webform by submission
$submission = webform_get_submission($nid, $sid);

// Webform's arrays, process it out
foreach($submission->data[1]['value'] as $code){
  if(!$code) { return; }
    return $code;
        }
    }
}

jakecivi

  • I post frequently
  • ***
  • Posts: 140
  • Karma: 0
Re: Buildamount hook clarfication
October 01, 2012, 12:25:22 am
Bump - any luck, anyone?  I'm having the same problem after installing my Drupal module on a new 4.2.2 - Drupal 6 installation.  Worked in 4.1 and Drupal 6.

Update: Yes, it's changed.  Here's the output of print_r($amount) in hook buildAmount:

Code: [Select]
Array
(
    [2] => Array
        (
            [id] => 2
            [name] => Event Fee(s)
            [label] => Event Fee(s)
            [html_type] => Radio
            [is_enter_qty] => 0
            [weight] => 1
            [is_display_amounts] => 1
            [options_per_line] => 1
            [is_active] => 1
            [visibility] => public
            [visibility_id] => 1
            [is_required] => 1
            [options] => Array
                (
                    [2] => Array
                        (
                            [id] => 2
                            [price_field_id] => 2
                            [name] => Dummy_fee
                            [label] => Dummy fee
                            [amount] => 10
                            [weight] => 1
                            [is_default] => 1
                            [is_active] => 1
                        )

                )

        )

    // And here's what my code did, as per the 4.0 hook specification,
    // which used to work but doesn't anymore:
    [100] => Array
        (
            [value] => 375
            [label] => Current fee
            [amount_id] => 100
        )

)

Another update:  I got past the initial hurdle by putting my information into the ['options'] array above with appropriate changes (amount_id changed to id) and unsetting the existing element.  However, CiviCRM code changes have made this orders of magnitude more complicated:

 - All prices are parts of price sets.
 - I got the buildAmount to work, but then upon confirming the registration, it gives a cannot-go-any-further error.  A look at the log reveals foreign key constraint issues involving civicrm_line_item and price_field_value_id.  Basically I'm making up an option in a price set that doesn't exist.
 - It seems the registration was created anyway.  Code I wrote to modify the Edit participant screen to allow an admin editing the fee works too, except it leaves the Fee Level displaying as "Array" rather than what it's supposed to be ("Current fee").

My module calculates a fee based on a form on a per-participant basis.  The fee can be different for every participant - yet the dummy fee I created sits with a price of $10 in civicrm_price_field_value.  This wasn't a problem in Civi 4.1, which didn't use price sets for simple fees - I could just override the fee option and be done with it.  Any recommendations on what to do with this?
« Last Edit: October 01, 2012, 01:25:42 am by jakecivi »

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: Buildamount hook clarfication
October 01, 2012, 09:17:36 am

seems like we skipped / forgot about this when we upgraded all "amounts" to use price sets

can you take a look at the cividiscount module:

https://github.com/dlobo/org.civicrm.module.cividiscount

and see how it handles price sets and discounts. I do believe we have discounting working well with this module on pricesets

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

jakecivi

  • I post frequently
  • ***
  • Posts: 140
  • Karma: 0
Re: Buildamount hook clarfication
October 01, 2012, 02:36:48 pm
Thank you, dlobo!  I had success substituting the following code for what I had in Civi 4.1:

Code: [Select]
foreach($amounts as &$fee) { // There's only one
  foreach($fee['options'] as &$option) { // There's only one
    $option['amount'] = $value;
    $option['label'] = $label;
    $option['name'] = $name;
    $option['id_default'] = 1;
  }
}

Now here's another question:  I also wrote some code for the Participant edit screen which allows an admin to edit the Participant record's fee_amount entry.  I know the display gets funny, but do I really need to edit the associated line_items too?  If not, the display from the line item could just be considered the "originally calculated" fee.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Buildamount hook clarfication

This forum was archived on 2017-11-26.