$custom_fields is not available in invoice templates and email template

I tried it in my testenvironment (running version 1.52) but there it does not work. Can’t test it in my production environment right now. Could you send an exact copy of the lines 209 - 212 that worked for you because maybe i made a typo somewhere…

Hi Benny,

Actually, the code is not correct. I’m working on it right now.

EDIT : Could you please confirm that the following code works for you too ?

File application\modules\custom_fields\models\Mdl_custom_fields.php

public function get_value_for_field($field_id, $custom_field_model, $object)
{
    $this->load->model('custom_fields/' . $custom_field_model);

    $cf_table = str_replace('mdl_', '', $custom_field_model);
    $cf_model_name = str_replace('_custom', '', $cf_table);

    $value = $this->$custom_field_model
        ->where($cf_table . '_fieldid', $field_id)
        ->where($cf_model_name . '_id', $object->{$cf_model_name . '_id'})
        ->get()->result();

    $value_key = $cf_table . '_fieldvalue';

    if (!isset($value[0]->$value_key)) {
        return '';
    }

    return $value[0]->$value_key;
}

File application\helpers\template_helper.php
Function function parse_template($object, $body, $model_id)

default:
// Check if it's a custom field
if (preg_match('/ip_cf_([0-9].*)/', $var, $cf_id)) {
	// Get the custom field
	$CI =& get_instance();
	$CI->load->model('custom_fields/mdl_custom_fields');
	$cf = $CI->mdl_custom_fields->get_by_id($cf_id[1]);

	if ($cf) {
		// Get the values for the custom field
		$cf_model = str_replace('ip_', 'mdl_', $cf->custom_field_table);
		$replace = $CI->mdl_custom_fields
			->get_value_for_field($cf_id[1], $cf_model, $object);
	} else {
		$replace = '';
	}
} else {
	$replace = isset($object->{$var}) ? $object->{$var} : $var;
}

Hi OlivierMa, This looks great. I did a quick test in my v1.52 testenvironment with the customer field translation in the e-mail body and it works like a charm. Great job! I do not know if you are one of the developers but otherwise i hope this fix will be picked up by the development team.

The fix will be available in the next release.

Hello, I just tested the code you sent. I’m trying to add a custom field of single select to my email template, in order to make the distinction between “Dear Mrs” or “Dear Sir”. Unfortunately the custom field translates into its key rather than its value, so I see “Dear 1” or “Dear 2”.
Thanks a lot

these two fixes did not work for me.
My custom field still outputs this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$client_custom_site_contact

EDIT: it’s not working in pdf or public invoices/quotes
I got it to work in emails by changing my custom field label to {{{ip_cf_2}}}

I recently upgrade the system to 1.5.4. and the $custom_fields is still not available in invoicePlane_Web.php public template.

I’m seeing the same thing, custom_fields is undefined in the public template.

Anyone coming across this; I made a pull request to make custom fields available in the web/public templates: https://github.com/InvoicePlane/InvoicePlane/pull/549/files

hello,
nothing is working for me, I can’t add a custom field to InvoicePlane_Web.php
not with changes to application\helpers\template_helper.php and application\modules\custom_fields\models\Mdl_custom_fields.php
nor with application/modules/guest/controllers/View.php

did it work for you all ?

hello,

sorry, I know why, maybe you need to reapply same changes in View.php to include also custom tags for quotes not only invoices.
I modified it and it works now.

Really…This needs to be tested on all environments before releasing…Very poorly managed.

Does latest version 1.5.5 have custom fields working?

Custom fields are working perfectly fine for me.

Hello, Did you find a fix for this?
Thanks

Issue seems to be with customs fields with “Single Choice” in E-Mail templates as I came across. It just displays the “custom_values_id” in “ip_custom_values” table.
Value in “ip_invoice_custom” table referring to the filed is also the same value. Is it correct?
Text and other single types work.
@YeyDev any help from your side is also welcome

I’ve got this problem as well. Just migrated from 1.4 to 1.5.9 and trying to solve this. Have read the docs on migrating & changes to custom fields. I have custom fields working in my PDF templates and in email, but am getting errors / no result in the web view templates.

For a custom variable called “ABN” in “user”, I’ve tried:

<?php if ($custom_fields['user']['ABN']) {
  echo 'ABN: ' . ($custom_fields['user']['ABN']) . '<br><br>'; } ?>

Which throws up a PHP error: “Undefined variable: custom_fields”

I’ve also tried:

<?php if (!empty($custom_fields['user']['ABN'])): ?>
  <?php echo 'ABN: ' . ($custom_fields['user']['ABN']) . '<br><br>'; ?>
<?php endif; ?>

Which doesn’t throw an error, but doesn’t print the custom variable.

What am I doing differently to you @Kovah ?

Custom fields are working as expected for both web and PDF templates.

Email templates are using a different approach that makes usage of some custom fields impossible at the moment. But email templates will be completely rewritten anyway.

OK, if they’re working correctly, how can I call them in web templates?

<?php print_r($custom_fields) ?>

throws:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: custom_fields

See my post here: Custom Fields Not Available in Quote Web Template