<?xml version="1.0" encoding="utf-8"?>
<modification>
	
	<name><![CDATA[TK Add order mail to admin with custom field]]></name>
	<version>1.1</version>
	<link>https://tankoo.eu</link>
	<author><![CDATA[<a href="https://tankoo.eu" target="_blank"><b>Tankoo.eu</b></a>]]></author>
	<code>tk_add_order_mail_admin_custom_field_1_1</code>
	
	<file path="admin/controller/sale/order.php">
		<operation error="skip">
			<search><![CDATA[$product_data = array();]]></search>
			<add position="before"><![CDATA[
			
			$data['text_account_custom_field'] = $this->language->get('text_account_custom_field');
			$data['text_payment_custom_field'] = $this->language->get('text_payment_custom_field');
			$data['text_shipping_custom_field'] = $this->language->get('text_shipping_custom_field');
			
			// Custom Fields
			$this->load->model('customer/custom_field');

			$data['account_custom_fields'] = array();

			$filter_data = array(
				'sort'  => 'cf.sort_order',
				'order' => 'ASC'
			);

			$custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'account' && isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['account_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['account_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['account_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['custom_field'][$custom_field['custom_field_id']]
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['account_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name']
							);
						}
					}
				}
			}

			// Custom fields
			$data['payment_custom_fields'] = array();

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'address' && isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['payment_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['payment_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['payment_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name'],
									'sort_order' => $custom_field['sort_order']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['payment_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['payment_custom_field'][$custom_field['custom_field_id']],
							'sort_order' => $custom_field['sort_order']
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['payment_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}
				}
			}

			// Shipping
			$data['shipping_custom_fields'] = array();

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'address' && isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['shipping_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['shipping_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['shipping_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name'],
									'sort_order' => $custom_field['sort_order']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['shipping_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['shipping_custom_field'][$custom_field['custom_field_id']],
							'sort_order' => $custom_field['sort_order']
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['shipping_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}
				}
			}
	]]></add>
		</operation>
		
	</file>
	
	<file path="admin/view/template/sale/order_shipping.tpl">
		<operation error="skip">
			<search index="2"><![CDATA[</table>]]></search>
			<add position="after"><![CDATA[
				<?php if ($account_custom_fields && isset($account_custom_fields[0]['value']) && $account_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_account_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($account_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>
		            <?php if ($payment_custom_fields && isset($payment_custom_fields[0]['value']) && $payment_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_payment_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($payment_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>
		            <?php if ($shipping_custom_fields && isset($shipping_custom_fields[0]['value']) && $shipping_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_shipping_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($shipping_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>
				]]></add>
		</operation>
		
	</file>
	
	<file path="admin/view/template/sale/order_info.tpl">
		<operation error="skip">
			<search index="4"><![CDATA[</table>]]></search>
			<add position="after"><![CDATA[
				<?php if ($account_custom_fields && isset($account_custom_fields[0]['value']) && $account_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_account_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($account_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>
		            <?php if ($payment_custom_fields && isset($payment_custom_fields[0]['value']) && $payment_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_payment_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($payment_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>
		            <?php if ($shipping_custom_fields && isset($shipping_custom_fields[0]['value']) && $shipping_custom_fields[0]['value']) { ?>
		            <div class="table-responsive">
		              <table class="table table-bordered">
		                <thead>
		                  <tr>
		                    <td colspan="2"><?php echo $text_shipping_custom_field; ?></td>
		                  </tr>
		                </thead>
		                <tbody>
		                  <?php foreach ($shipping_custom_fields as $custom_field) { ?>
		                  <tr>
		                    <td><?php echo $custom_field['name']; ?></td>
		                    <td><?php echo $custom_field['value']; ?></td>
		                  </tr>
		                  <?php } ?>
		                </tbody>
		              </table>
		            </div>
		            <?php } ?>

				]]></add>
		</operation>
		
	</file>
	
	<file path="catalog/model/account/custom_field.php">
		<operation error="skip">
			<search><![CDATA[public function getCustomFields($customer_group_id = 0)]]></search>
			<add position="before"><![CDATA[
				public function getCustomFieldValue($custom_field_value_id) {
					$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "custom_field_value cfv LEFT JOIN " . DB_PREFIX . "custom_field_value_description cfvd ON (cfv.custom_field_value_id = cfvd.custom_field_value_id) WHERE cfv.custom_field_value_id = '" . (int)$custom_field_value_id . "' AND cfvd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

					return $query->row;
				}
				]]></add>
		</operation>
	</file>
	
	<file path="catalog/model/checkout/order.php">
		<operation error="skip">
			<search><![CDATA[$data['totals'] = array();]]></search>
			<add position="before"><![CDATA[
			// Custom Fields
			$this->load->model('account/custom_field');

			$data['account_custom_fields'] = array();

			$filter_data = array(
				'sort'  => 'cf.sort_order',
				'order' => 'ASC'
			);

			$custom_fields = $this->model_account_custom_field->getCustomFields($filter_data);

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'account' && isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($order_info['custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['account_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['account_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['account_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['custom_field'][$custom_field['custom_field_id']]
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['account_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name']
							);
						}
					}
				}
			}

			// Custom fields
			$data['payment_custom_fields'] = array();

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'address' && isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['payment_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['payment_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['payment_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name'],
									'sort_order' => $custom_field['sort_order']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['payment_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['payment_custom_field'][$custom_field['custom_field_id']],
							'sort_order' => $custom_field['sort_order']
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['payment_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}
				}
			}

			// Shipping
			$data['shipping_custom_fields'] = array();

			foreach ($custom_fields as $custom_field) {
				if ($custom_field['location'] == 'address' && isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
						$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

						if ($custom_field_value_info) {
							$data['shipping_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $custom_field_value_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}

					if ($custom_field['type'] == 'checkbox' && is_array($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
						foreach ($order_info['shipping_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
							$custom_field_value_info = $this->model_account_custom_field->getCustomFieldValue($custom_field_value_id);

							if ($custom_field_value_info) {
								$data['shipping_custom_fields'][] = array(
									'name'  => $custom_field['name'],
									'value' => $custom_field_value_info['name'],
									'sort_order' => $custom_field['sort_order']
								);
							}
						}
					}

					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
						$data['shipping_custom_fields'][] = array(
							'name'  => $custom_field['name'],
							'value' => $order_info['shipping_custom_field'][$custom_field['custom_field_id']],
							'sort_order' => $custom_field['sort_order']
						);
					}

					if ($custom_field['type'] == 'file') {
						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

						if ($upload_info) {
							$data['shipping_custom_fields'][] = array(
								'name'  => $custom_field['name'],
								'value' => $upload_info['name'],
								'sort_order' => $custom_field['sort_order']
							);
						}
					}
				}
			}
				]]></add>
		</operation>
	</file>
	
	<file path="catalog/view/theme/*/template/mail/order.tpl">
		<operation error="skip">
			<search><![CDATA[<p style="margin-top: 0px; margin-bottom: 20px;"><?php echo $text_footer; ?></p>]]></search>
			<add position="before"><![CDATA[
			<?php if ($account_custom_fields && isset($account_custom_fields[0]['value']) && $account_custom_fields[0]['value']) { ?>
            <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">
                <tbody>
                  <?php foreach ($account_custom_fields as $custom_field) { ?>
                  <tr>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['name']; ?></td>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['value']; ?></td>
                  </tr>
                  <?php } ?>
                </tbody>
              </table>
            <?php } ?>
            <?php if ($payment_custom_fields && isset($payment_custom_fields[0]['value']) && $payment_custom_fields[0]['value']) { ?>
            <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">
                <tbody>
                  <?php foreach ($payment_custom_fields as $custom_field) { ?>
                  <tr>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['name']; ?></td>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['value']; ?></td>
                  </tr>
                  <?php } ?>
                </tbody>
              </table>
            <?php } ?>
            <?php if ($shipping_custom_fields && isset($shipping_custom_fields[0]['value']) && $shipping_custom_fields[0]['value']) { ?>
            <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">
                <tbody>
                  <?php foreach ($shipping_custom_fields as $custom_field) { ?>
                  <tr>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['name']; ?></td>
                    <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;"><?php echo $custom_field['value']; ?></td>
                  </tr>
                  <?php } ?>
                </tbody>
              </table>
            <?php } ?>
				]]></add>
		</operation>
	</file>
	
	
</modification>