2

Drupal 7.37, CiviCRM 4.4.15

Principles outlined in this page to embed a variable in the URL that gets stored in a hidden field on the contribution page.

Do I need to make the custom fields required or read-only? I made them searchable. For each contribution I am getting a row inserted into the database table "civicrm_value_campaign_1", but the three fields are all blank.

URL: www.domain.org/civicrm/contribute/transact?reset=1&id=8&custom_1=sponsorship&custom_2=15&custom_3=child&amount=35.00

<?php

function pptsponsorship_civicrm_buildForm($formName, &$form)
{
// enable tracking feature
if (($formName == 'CRM_Contribute_Form_Contribution_Main' ||
        $formName == 'CRM_Contribute_Form_Contribution_Confirm' ||
        $formName == 'CRM_Contribute_Form_Contribution_ThankYou') &&
    $form->getVar('_id') == 8 ) { // use  CONTRIBUTION PAGE ID here

    // use the custom field ID and custom field label here
    $trackingFields = array('custom_1' => 'Campaign',
        'custom_2' => 'Appeal',
        'custom_3' => 'Fund');
    $form->assign('trackingFields', $trackingFields);

    if ($_GET['amount']) {
        $defaults['price_3'] = $_GET['amount']; //set the other value
        $defaults['frequency_interval'] = 1;
    }
    $form->setDefaults($defaults);
}
}  

changed the code to the following:

function pptsponsorship_civicrm_buildForm($formName, &$form) { // enable tracking feature if (($formName == 'CRM_Contribute_Form_Contribution_Main' || $formName == 'CRM_Contribute_Form_Contribution_Confirm' || $formName == 'CRM_Contribute_Form_Contribution_ThankYou') && $form->getVar('_id') == 8 ) { // use CONTRIBUTION PAGE ID here

    if (!$_GET['custom_1']) {
        require_once 'CRM/Core/BAO/CustomGroup.php';
        $defaults = CRM_Core_BAO_CustomGroup::extractGetParams( $this, "'Contact', 'Individual', 'Contribution'" );
    } else {
        $defaults['custom_1'] = $_GET['custom_1'];
        $defaults['custom_2'] = $_GET['custom_2'];
        $defaults['custom_3'] = $_GET['custom_3'];
    }

    // use the custom field ID and custom field label here
    $trackingFields = array('custom_1' => 'Campaign:Campaign',
        'custom_2' => 'Campaign:Appeal',
        'custom_3' => 'Campaign:Fund');
    $form->assign('trackingFields', $trackingFields);

    if ($_GET['amount']) {
        $defaults['price_3'] = $_GET['amount'];
        $defaults['frequency_interval'] = 1;
    }
    $form->setDefaults($defaults);
}

}

It is now working.

  • 1
    How are you populating your hidden fields? Are you using a jQuery snippet like the one linked to from the page you mentioned? If so, please update your question with that code. Also, this may not be ready in time for you, but some of what you want is coming to core: forum.civicrm.org/index.php?topic=36091.0 – Jon G - Megaphone Tech Jun 18 '15 at 3:19
  • Missed that small step at the beginning. Thank you – mike_kc Jun 18 '15 at 22:49
  • Glad that helped. I'm going to leave that as an answer, so as not to leave a question in the "unanswered" queue. – Jon G - Megaphone Tech Jun 18 '15 at 23:14
1

It looks like you're not using the jQuery snippet mentioned here to populate your hidden values.

Also, there's a Google Summer of Code 2015 project that will hopefully make this question obsolete: Also, this may not be ready in time for you, but some of what you want is coming to core: forum.civicrm.org/index.php?topic=36091.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.