thank you @UnderDog, it helped me understand it a little better and var_dump() helped me immensely.
I managed to copy my custom fields from quote to invoice now. So Iâm posting this for anyone whoâs trying to achieve the same thing or wants to improve the code .
I have written the following lines from around line 378 in â/application/modules/quotes/controllers/ajax.php. after the quote_tax_rate foreach loop.
$this->load->model('custom_fields/mdl_quote_custom');
$this->load->model('custom_fields/mdl_quote_custom');
$CI = &get_instance();
$CI->load->model('custom_fields/mdl_custom_fields');
$db_array = $CI->mdl_custom_fields->get_values_for_fields('mdl_quote_custom', $quote->quote_id);
$formatted_date = date_from_mysql($db_array['myCustomDate'], true);
$customCopy = array();
if (array_key_exists('myCustom1', $db_array)){
$customCopy[14] = $db_array['myCustom1'];}
else {$customCopy[14] = null;}
if (array_key_exists('myCustom2', $db_array)){
$customCopy[13] = $db_array['myCustom2'];}
else {$customCopy[13] = null;}
if (array_key_exists('myCustomDate', $db_array)){
$customCopy[16] = $formatted_date;}
if (array_key_exists('myCustom3', $db_array)){
$customCopy[15] = $db_array['myCustom3'];}
else {$customCopy[15] = null;}
if (array_key_exists('myCustom4.', $db_array)){
$customCopy[25] = $db_array['myCustom4.'];}
else {$customCopy[25] = null;}
$customCopy[26] = null;
$this->load->model('custom_fields/mdl_invoice_custom');
$result = $this->mdl_invoice_custom->save_custom($invoice_id, $customCopy);
EDIT: I ran into some problems when I didnât declare all invoice custom fields. The database then changes some values and some of your fields might get deleted. So its important to add them to your array already. Like in the example above $customCopy[26] = null;
this field does not exist for quotes but it does for invoices so I just set it to null. If it was a boolean just set it to either 1 or 0 depending on if you want it to be true or false. I also left the order of the array exactly how it prints and didnât reorder them. I dont think this is important because I think they will get reordered anyway but better be safe.
I know this isnât the prettiest code and I should check if myCustomFieldX also exists for invoices and then copy it over, but I couldnât get that to work and this solution works for me.
If anyone wants to implement this themselves, you have to find out the custom_field_id of your custom field. This is NOT the order number you manually give your custom field when creating it. You can do this a couple of ways. Either check directly in your db or copy the following code in âapplication/modules/invoices/views.phpâ
<?php
$fieldValue = $this->mdl_invoices->form_value('custom[' . $custom_field->custom_field_id . ']');
echo '<script>';
echo 'console.log('. json_encode( $custom_field ).')';
echo '</script>';
?>
once at line 409 after:
<!-- Custom fields -->
<?php foreach ($custom_fields as $custom_field): ?>
<?php if ($custom_field->custom_field_location != 1) {
continue;
} ?>
and once at line 549 after the (almost) identical code block.
Now if you open up one of your invoices in your browser it will print your custom fields to the console with your custom_field_id (plus some more information).
You need to fit your ids from the invoice to the ones from the quotes.
So in the example above it would be:
$customCopy ['3'] => $db_array['Liefertermin'];
This worked for me. I know itâs not the ideal way but maybe it will help someone.