The participant_id can be accessed on the event registration thank you page in two different ways -- by using Smarty template variables when the user is logged in or by using session variables when the user is anonymous. Both approaches work when used inside the "civicrmBuildForm" hook.
1) User logged in
add_action( 'civicrm_buildForm', 'getParticipantId' );
function getParticipantId() {
$template = (array) CRM_Core_Smarty::singleton();
$event = $template['_tpl_vars']['event'];
$participant = civicrm_api3('Participant', 'getsingle', [
'sequential' => 1,
'contact_id' => "user_contact_id",
'event_id' => $event['id'],
]);
echo $participant['id'];
}
2) Anonymous user
add_action( 'civicrm_buildForm', 'getParticipantId' );
function getCrmFromSession($session) {
if (!empty($session)) {
$seshAry = (array) $session;
$seshKey = array_keys($seshAry)[1];
$CiviCRM = json_decode(json_encode($seshAry[$seshKey]), true)['CiviCRM'];
if ( !empty($CiviCRM['CRM_Utils_Recent']) && isset($CiviCRM['CRM_Utils_Recent'][0]['id']) ) {
$CiviCRM['participant_id'] = $CiviCRM['CRM_Utils_Recent'][0]['id'];
}
return $CiviCRM;
} else {
return array();
}
}
function getParticipantId() {
$CiviCRM = getCrmFromSession(CRM_Core_Session::singleton());
echo $CiviCRM['participant_id'];
}
- I appended a template to the Thank You template with the data related to participant_id.
- In my case, the BuildForm hook lives in my theme's functions.php file.
{debug}
at any line in the file and check the variables that are populating for the template file.