Custom Fields Not Available in Quote Web Template

Version 1.5.9

Trying to build a custom template for quotes however when I include the $custom_fields variable I get an error that it is not defined. Is this a known bug or am I doing something wrong?

1 Like

I seem to have this problem as well. Just migrated from 1.4 and trying to solve this. Have read the docs on migrating & changes to custom fields.

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.

Any ideas?

I cannot reproduce this problem.
Adding

<?php
echo '<pre>';
var_dump($custom_fields);
echo '</pre>';
?>

prints out

array (size=3)
  'invoice' => 
    array (size=3)
      'Sent on' => string '2018-04-26' (length=10)
      'Test Custom Field 2' => null
      'Test Single Choice' => string '' (length=0)
  'client' => 
    array (size=0)
      empty
  'user' => 
    array (size=0)
      empty

which is correct for my testing setup.

Please verify that the contents of your file application/modules/guest/controllers/View.php matches this file.

Thanks for the reply. That code again just prints the same error for me.

And yes, checked my live production version of View.php and the one from Git and they are identical.

Mystery! What else could be causing this?

that is weird indeed. If both files are identical, the custom_fields variable should be available in the templates. Have no idea why the var could be missing.

Ive got the same problem with getting custom field from user.
Same line of code works in invoice web view but not in quote.

<?php echo $custom_fields['user']['oznaka_operatera']; ?>

an error in quote web template

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: custom_fields
Filename: public/quote-web.php
Line Number: 269
Backtrace:
File: /public_html/application/views/quote_templates/public/quote-web.php
Line: 269
Function: _error_handler

File: /public_html/application/third_party/MX/Loader.php
Line: 464
Function: include

File: /public_html/application/third_party/MX/Loader.php
Line: 415
Function: _ci_load

File: /public_html/application/modules/guest/controllers/View.php
Line: 208
Function: view

File: /public_html/index.php
Line: 325
Function: require_once

IP v1.5.9

edit:
@Kovah

<?php
echo '<pre>';
var_dump($custom_fields);
echo '</pre>';
?>

returns error:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: custom_fields
Filename: public/quote-web.php
Line Number: 297
Backtrace:
File: /public_html/application/views/quote_templates/public/quote-web.php
Line: 297
Function: _error_handler

File: /public_html/application/third_party/MX/Loader.php
Line: 464
Function: include

File: /public_html/application/third_party/MX/Loader.php
Line: 415
Function: _ci_load

File: /public_html/application/modules/guest/controllers/View.php
Line: 208
Function: view

File: /public_html/index.php
Line: 325
Function: require_once

NULL 

same code in invoice web template returns an array of custom fields.

found the solution
in file application/modules/guest/controllers/View.php after line 170 add

$this->load->model('custom_fields/mdl_custom_fields');

after

if ($this->session->userdata('user_type') <> 1 and $quote->quote_status_id == 2) {
            $this->mdl_quotes->mark_viewed($quote->quote_id);
        }

add

// Get all custom fields
        $custom_fields = array(
            'quote' => $this->mdl_custom_fields->get_values_for_fields('mdl_invoice_custom', $quote->quote_id),
            'client' => $this->mdl_custom_fields->get_values_for_fields('mdl_client_custom', $quote->client_id),
            'user' => $this->mdl_custom_fields->get_values_for_fields('mdl_user_custom', $quote->user_id),
        );

and add

    'custom_fields' => $custom_fields,

to array

$data = array(
    'quote' => $quote,
    'items' => $this->mdl_quote_items->where('quote_id', $quote->quote_id)->get()->result(),
    'quote_tax_rates' => $this->mdl_quote_tax_rates->where('quote_id', $quote->quote_id)->get()->result(),
    'quote_url_key' => $quote_url_key,
    'flash_message' => $this->session->flashdata('flash_message'),
    'is_expired' => $is_expired,
    'attachments' => $attachments,
);

this is my working version of View.php -> https://pastebin.com/LbKbxvzG

Cheers,
G.

1 Like

Thanks @gregorb for solving this issue. The next steps should be:

  • to open a ticket at the development portal and
  • to submit a pull-request on GitHub with your fixes so that they can be incorporated into the next release.

If you don’t want/plan to do it for whatever reason, let us know and I will do it on behalf of you and will keep your name as contributor.

BTW, you should mark your last post as accepted answer.

1 Like

Ticket at the development portal:

  • IP-741 - Custom Fields Not Available in Quote Web Template

and pull request in GitHub:

In the file application/modules/guest/controllers/View.php change the “public function quote” to be like this:

public function quote($quote_url_key = '')
    {
        if (!$quote_url_key) {
            show_404();
        }

        $this->load->model('quotes/mdl_quotes');

        $quote = $this->mdl_quotes->guest_visible()->where('quote_url_key', $quote_url_key)->get();

        if ($quote->num_rows() != 1) {
            show_404();
        }

        $this->load->model('quotes/mdl_quote_items');
        $this->load->model('quotes/mdl_quote_tax_rates');
        $this->load->model('custom_fields/mdl_custom_fields');

        $quote = $quote->row();

        if ($this->session->userdata('user_type') <> 1 and $quote->quote_status_id == 2) {
            $this->mdl_quotes->mark_viewed($quote->quote_id);
        }
        
        // Get all custom fields
        $custom_fields = array(
            'quote' => $this->mdl_custom_fields->get_values_for_fields('mdl_quote_custom', $quote->quote_id),
            'client' => $this->mdl_custom_fields->get_values_for_fields('mdl_client_custom', $quote->client_id),
            'user' => $this->mdl_custom_fields->get_values_for_fields('mdl_user_custom', $quote->user_id),
        );

        // Attachments
        $attachments = $this->get_attachments($quote_url_key);
        /*$path = '/uploads/customer_files';
        $files = scandir(getcwd() . $path);
        $attachments = array();

        if ($files !== false) {
            foreach ($files as $file) {
                if ('.' != $file && '..' != $file && strpos($file, $quote_url_key) !== false) {
                    $obj['name'] = substr($file, strpos($file, '_', 1) + 1);
                    $obj['fullname'] = $file;
                    $obj['size'] = filesize($path . '/' . $file);
                    $obj['fullpath'] = base_url($path . '/' . $file);
                    $attachments[] = $obj;
                }
            }
        }*/

        $is_expired = (strtotime($quote->quote_date_expires) < time() ? true : false);

        $data = array(
            'quote' => $quote,
            'items' => $this->mdl_quote_items->where('quote_id', $quote->quote_id)->get()->result(),
            'quote_tax_rates' => $this->mdl_quote_tax_rates->where('quote_id', $quote->quote_id)->get()->result(),
            'quote_url_key' => $quote_url_key,
            'flash_message' => $this->session->flashdata('flash_message'),
            'is_expired' => $is_expired,
            'attachments' => $attachments,
            'custom_fields' => $custom_fields,
        );

        $this->load->view('quote_templates/public/' . get_setting('public_quote_template') . '.php', $data);
    }

I found the error here:

// Get all custom fields
$custom_fields = array(
‘quote’ => $this->mdl_custom_fields->get_values_for_fields(‘mdl_invoice_custom’, $quote->quote_id),
‘client’ => $this->mdl_custom_fields->get_values_for_fields(‘mdl_client_custom’, $quote->client_id),
‘user’ => $this->mdl_custom_fields->get_values_for_fields(‘mdl_user_custom’, $quote->user_id),
);

It should be

// Get all custom fields
        $custom_fields = array(
            'quote' => $this->mdl_custom_fields->get_values_for_fields('mdl_quote_custom', $quote->quote_id),
            'client' => $this->mdl_custom_fields->get_values_for_fields('mdl_client_custom', $quote->client_id),
            'user' => $this->mdl_custom_fields->get_values_for_fields('mdl_user_custom', $quote->user_id),
        );

You can add your custom field to your web or pdf template this way:

<!-- Custom fields -->
<table>
<?php foreach ($custom_fields['quote'] as $custom_quote_field_name => $custom_quote_field_value) : ?>
                       <tr>
                           <td>
                                <?php echo htmlsc($custom_quote_field_name); ?>
                            </td>
                            <td>
                                <?php echo htmlsc($custom_quote_field_value); ?>
                            </td>
                        </tr>
                       <?php endforeach; ?>
</table>
2 Likes

Thank you.

You can log a bug report and submit the change for inclusion into the code base.

1 Like

Hi @josip_Ivos thank you for finding this code for us. Let’s see if we can include it in the 1.5.10 release.

2 Likes