Support

Home Forums Event Espresso Premium Custom field in message template file

Custom field in message template file

Posted: October 9, 2017 at 3:57 pm


asteiner

October 9, 2017 at 3:57 pm

Hi,

How I can use custom field in message template file? (invoice_body.template.php)

I tried this, but it does not work:
<?php echo do_shortcode('[PRIMARY_REGISTRANT_ANSWER_* Firma]'); ?>

Thank you.


Tony

  • Support Staff

October 10, 2017 at 2:21 am

The messages shortcodes do not use the WP Shortcode API so do_shortcode() will not work with those.

Where are you editing invoice_body.template.php? EE allows you to edit the templates within the admin so generally you don’t need to modify files directly.


asteiner

October 10, 2017 at 7:13 am

I am editing it in wp-content/uploads/espresso/templates/modules/gateways/Invoice/lib/templates

I needed a bit custom Invoice template, change layout a bit, hide some elements. So I am using this one..

Any way I can get this custom field to this custom template??


Josh

  • Support Staff

October 10, 2017 at 1:54 pm

There may be. Does your custom template have access to the $transaction object? You’ll see something like this:
/* @var $transaction EE_Transaction */

Then you could try something like this:

$primary_registration = $transaction->primary_registration();

foreach ( $primary_registration->questions as $ansid => $question ) {
	if (
		trim( $question->get( 'QST_display_text' ) ) == trim( 'Your question text here' )
		&& isset( $primary_registration->registrations[ $primary_reg->ID() ]['ans_objs'][ $ansid ] )
	) {
		echo $primary_registration->registrations[ $primary_reg->ID() ]['ans_objs'][ $ansid ]->get_pretty( 'ANS_value', 'no_wpautop' );
	}
}


asteiner

October 11, 2017 at 7:24 am

$transaction->primary_registration() is available but $primary_registration->questions returns NULL


Josh

  • Support Staff

October 11, 2017 at 7:27 am

$transaction->primary_registration() is available but $primary_registration->questions returns NULL

Not available?

It sounds like you removed some code from the original template then maybe?


asteiner

October 11, 2017 at 7:38 am

As I have mentioned $primary_registration = $transaction->primary_registration(); is available but not questions which is NULL

invoice_body.template.php content (you probably need to paste this to text editor for readability)

<?php
/**
* @deprecated 4.9.13
*/
?>
<div id="invoice">

		<table id="invoice-header" class="not-really-a-table">
			<tr>
				<td id="logo-dv">
					[invoice_logo_image]
					<div class="vcard" id="company-address">
						<div class="fn org"><strong>[organization]</strong></div>
						<div class="adr">
							<div class="street-address">[street]
							</div>
							<!-- street-address -->
							<div class="locality"><span class="postal-code">[zip]</span> [city]</div>
						</div>
					</div>
				</td>
				<td>
					<div id="invoice-info">
						<h2 id="invoice-hdr"><?php _e('Invoice', 'event_espresso')?></h2>
								<table class="not-really-a-table">
									<tr>
										<td class="line-name"><?php _e('Date:', 'event_espresso')?></td>
										<td>[registration_date]</td>
									</tr>
										<tr>
											<td class="line-name"><?php _e('Registration Code:', 'event_espresso')?></td>
											<td>[registration_code]</td>
										</tr>
											<tr>
												<td class="line-name"><?php _e('Transaction ID:', 'event_espresso')?></td>
												<td>[transaction_id]</td>
											</tr>
								</table>
					</div>
				</td>
			</tr>
			<tr>
				<td id="instructions" colspan="2">
					<p>[instructions]</p>
				</td>
			</tr>
		</table>
		<h2><?php _e('Bill To:', 'event_espresso')?></h2>
		<div class="vcard" id="client-details">
						<div class="fn"><?php 

						$primary_registration = $transaction->primary_registration();
//var_dump($transaction);
//var_dump($primary_registration);
						foreach ( $primary_registration->questions as $ansid => $question ) {
							if (
								trim( $question->get( 'QST_display_text' ) ) == trim( 'Firma' )
								&& isset( $primary_registration->registrations[ $primary_reg->ID() ]['ans_objs'][ $ansid ] )
							) {
								echo $primary_registration->registrations[ $primary_reg->ID() ]['ans_objs'][ $ansid ]->get_pretty( 'ANS_value', 'no_wpautop' );
							}
						}

						//echo do_shortcode('[PRIMARY_REGISTRANT_ANSWER_* Firma]'); 

						?></div>
						<div class="fn">[name]</div>
						<div class="adr">
							<div class="street-address"><?php echo $attendee_address; ?> <br><?php echo $attendee_address2; ?></div>
							<div class="locality"><?php echo $attendee_zip; ?> <?php echo $attendee_city; ?> <?php echo $attendee_state; ?></div>
							<!--<div id="client-postcode"><?php //echo $attendee_zip; ?></div>-->
						</div>
					</div>
					 <!--#client-details vcard-->

		<h2><?php _e("Purchases",'event_espresso')?></h2>
		<table class="invoice-amount">

			<thead>
				<tr class="header_row">
					<th class="left ticket_th"><?php _e('Item', 'event_espresso'); ?></th>
					<?php if ($show_line_item_description){?><th class="left event_th"><?php _e('Description', 'event_espresso'); ?></th><?php }?>
					<th class="quantity_th"><?php _e('Qty', 'event_espresso'); ?></th>
					<th class="item_c event_th"><?php _e('Price', 'event_espresso'); ?></th>
					<th class="subtotal_th item_r"><?php _e('Total', 'event_espresso'); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				/**
				 * Recursive function for traversing all the sub-items of each line item
				 * and displaying them in the table
				 * @param EE_Line_Item $line_item
				 * @param boolean $odd for indicating whether to style this line item as an 'odd' or 'even'
				 */
				function ee_invoice_display_line_item(EE_Line_Item $line_item, $show_line_item_description, $odd = false){
					switch($line_item->type()){
						case EEM_Line_Item::type_total:
							foreach($line_item->children() as $child_line_item){
								ee_invoice_display_line_item($child_line_item,$show_line_item_description);
								}?>
							<tr><td colspan="<?php echo $show_line_item_description ? 5 : 4 ?>"><hr></td></tr>
							<tr class="total_tr odd">
								<td colspan="<?php echo $show_line_item_description ? 2 : 1 ?>">&nbsp;</td>
								<td colspan="2" class="total" id="total_currency"><?php _e('Total', 'event_espresso'); ?></td>
								<td class="total"><?php echo $line_item->total_no_code();?></td>
							</tr>
						<?php break;

						case EEM_Line_Item::type_sub_total:
							foreach($line_item->children() as $child_line_item){
							//$odd = !$odd;
								ee_invoice_display_line_item($child_line_item,$show_line_item_description,$odd);
							}?>
							<tr class="total_tr odd">
								<td colspan="<?php echo $show_line_item_description ? 2 : 1 ?>">&nbsp;</td>
								<td colspan="2" class="total" id="total_currency"><?php _e('Sub-Total', 'event_espresso'); ?></td>
								<td class="total"><?php echo $line_item->total_no_code();?></td>
							</tr>
						<?php break;

						case EEM_Line_Item::type_line_item:
							$subitems = $line_item->children();
							$has_subitems  = count($subitems) > 1 ;
							if($has_subitems){
							?>
							<tr class="item <?php echo $odd ?   'odd' : ''; ?>">
								<td class="item_l"><?php echo $line_item->name() ?></td>
								<?php if ($show_line_item_description){?><td class="item_l"><?php echo $line_item->desc() ?></td><?php }?>
								<td class="item_l"><?php echo $line_item->quantity();?></td>

								<td class="item_c"><?php echo $line_item->unit_price_no_code()?></td>

								<td class="item_r"> <?php echo $line_item->total_no_code(); echo $line_item->is_taxable() ? '*' : ''?> </td>
								<?php //<td class="item_l"><?php  $datetimes_strings = array(); foreach($datetimes as $datetime){ $datetimes_strings[]= $datetime->start_date_and_time();} echo implode(", ",$datetimes_strings); ?>
							</tr>
							<?php
								if( $has_subitems ){
									foreach($line_item->children() as $child_line_item){
										ee_invoice_display_line_item($child_line_item,$show_line_item_description, $odd);
									}
								}
							}else{//no subitems - just show this line item ?>
								<tr class="item <?php echo $odd ?   'odd' : ''; ?>">
									<td class="item_l"><?php echo $line_item->name()?></td>
									<?php if ($show_line_item_description){?><td class="item_l"><?php echo $line_item->desc() ?></td><?php }?>
									<td class="item_l"><?php echo $line_item->quantity()?></td>
									<td class="item_c"><?php echo $line_item->unit_price_no_code()?></td>
									<td class="item_r"> <?php echo $line_item->total_no_code(); echo $line_item->is_taxable() ? '*' : ''?> </td>
									<?php //<td class="item_l"><?php  $datetimes_strings = array(); foreach($datetimes as $datetime){ $datetimes_strings[]= $datetime->start_date_and_time();} echo implode(", ",$datetimes_strings); ?>
								</tr>
							<?php }

						break;
						case EEM_Line_Item::type_sub_line_item:
							?>
						<tr class="item subitem-row">
							<td class="item_l subitem"><?php echo $line_item->name(); ?></td>
							<?php if ($show_line_item_description){?><td class="item_l"><?php echo $line_item->desc() ?></td><?php }?>
							<?php if ($line_item->is_percent()) { ?>
									<td></td>
									<td class="item_c"><?php echo $line_item->percent();?>%</td>
							<?php }else{//flat discount/surcharge ?>
									<td></td>
									<td class="item_c"><?php echo $line_item->unit_price_no_code();?></td>
							<?php } ?>
							<td class="item_r"><?php echo $line_item->total_no_code();?></td>
						</tr>
						<?php break;
					}
				}
				$c=false;
				/* @var $transaction EE_Transaction */
				$total_line_item = $transaction->total_line_item();
				ee_invoice_display_line_item($total_line_item,$show_line_item_description);
				/* foreach($transaction->registrations() as $registration){
					 ?>
					<tr class="item <?php echo ($c = !$c) ? ' odd' : ''; ?>">
						<td class="item_l">1</td>
						<td class="item_l"><?php echo $registration->event_name() ?></td>
						<td class="item_l"><?php echo $registration->ticket()->name() ?></td>
						<td class="item_l"><?php $datetimes = $registration->ticket()->datetimes(); $datetimes_strings = array(); foreach($datetimes as $datetime){ $datetimes_strings[]= $datetime->start_date_and_time();} echo implode(", ",$datetimes_strings); ?></td>
						<td class="item_l"><?php echo $registration->attendee()->full_name() ?></td>
						<td class="item_r"><?php echo EEH_Template::format_currency($registration->final_price())?></td>
					</tr>
				<?php } */ ?>
			</tbody>

		</table>

		<h2><?php _e("Payments",'event_espresso')?></h2>
		<table class="invoice-amount">
			<thead>
				<tr class="header_row">
					<th ><span class=""><?php _e('Payment Method', 'event_espresso'); ?></span></th>
					<th class='left datetime_th'><?php _e("Date",'event_espresso')?></th>
					<th ><span class=""><?php _e('Transaction Id / Cheque #', 'event_espresso'); ?></span></th>
					<th ><span class=""><?php _e('P.O. / S.O.#', 'event_espresso'); ?></span></th>
					<th ><span class=""><?php _e('Status', 'event_espresso'); ?></span></th>
					<th ><?php _e('Amount', 'event_espresso'); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				$c = false;
				if(!empty($payments)){

					foreach($payments as $payment){
						/* @var $payment EE_Payment */?>
					<tr class='item <?php echo (($c = !$c) ? ' odd' : '')?>'>
						<td><?php $payment->e('PAY_gateway')?></td>
						<td><?php echo $payment->timestamp('D M j, Y')?></td>
						<td><?php $payment->e('PAY_txn_id_chq_nmbr')?></td>
						<td><?php $payment->e('PAY_po_number')?></td>
						<td><?php $payment->e_pretty_status()?></td>
						<td class='item_r'><?php echo EEH_Template::format_currency($payment->amount());?></td>
					</tr>
					<?php }
				}else{?>
					<tr class='item'>
						<td class='aln-cntr' colspan=6><?php _e("No approved payments have been received",'event_espresso')?></td>
					</tr>
				<?php }
?>
			</tbody>
			<tfoot>
				<tr class='total_tr'><td colspan="4"></td>
					<td class="item_r"><?php _e('Total Paid','event_espresso')?></td>
					<td class="item_r"><?php echo EEH_Template::format_currency($amount_pd)?> </td>
				</tr>
				<?php //echo $discount; ?>
				<tr class="total_tr">
					<td colspan="4"></td>
					<td class="total" id="total_currency"><?php _e('Amount Owed', 'event_espresso'); ?></td>
					<td class="total"><?php echo EEH_Template::format_currency($total_cost - $amount_pd)?></td>
				</tr>
			</tfoot>
		</table>

	</div>


asteiner

October 11, 2017 at 7:39 am

This is custom field I am trying to get: https://trello-attachments.s3.amazonaws.com/583f025f63d8476a40bb1d3c/59552c71fa009869390551c7/6da91aab6042ac04ad9795042b1e1bec/Screen_Shot_2017-10-11_at_16.33.03.png


Tony

  • Support Staff

October 11, 2017 at 12:50 pm

I’d recommend switching to the new Invoice system rather than the old system you are currently using, you can continue to use the old system whislt editing the template from within the admin to apply all of your changes to there, then remove the above template (or rename the modules directory to test).

The invoice template can now be edited from within the admin and eventually, the old system will be phased out of Event Espresso (it is currently still within EE for legacy support).

The above code works in the new system as the new system creates an EE_Messages_Addressee object which pulls all of the questions/answers in for you, that doesn’t work on the registration object, you need to do it yourself. For example:

$primary_registration = $transaction->primary_registration();

$related_answers = $primary_registration->get_many_related('Answer');

$questions = $answers = array();

foreach ($related_answers as $ansid => $answer) {
    if (! isset($questions[$ansid])) {
        $questions[$ansid] = $answer->get_first_related('Question');
    }
}

foreach ( $questions as $ansid => $question ) {
	if ( trim( $question->get( 'QST_display_text' ) ) == trim( 'Firma' ) )
	{
		echo EEM_Answer::instance()->get_answer_value_to_question($primary_registration, $question->ID(), true); 
	}
}

You can loop through multiple questions and assign them to variable within the bottom loop, then output the variable where needed, or whatever else you prefer really.

Another option if you know the question ID is to hard code the question ID into the get_answer_value_to_question() and pull them directly:

EEM_Answer::instance()->get_answer_value_to_question($primary_registration, 111, true);

Would pull the answer for question ID 111 for the primary_registration object so you can use them.

However again, as mentioned above, the new system does all of this for you and wraps them in the shortcodes available within the admin. Customizing the templates for the old system (which is what you are currently doing) will eventually no longer be supported so either way, eventually you will need to move over to the new system.

The support post ‘Custom field in message template file’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: closed

Updated by  Tony 2 years, 1 month ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!
[for="input_96_5_3"]
[for="input_96_5_3"]
[gravityform id=96 title=false description=false]
  • This field is for validation purposes and should be left unchanged.
[i]
[i]
[gravityform id=69 title=false description=false]
  • This field is for validation purposes and should be left unchanged.