<?xml version="1.0" encoding="UTF-8"?>
<modification>
  <name>Пользовательские поля в адресе</name>
  <code>custom-fields</code>
  <version>2.0</version>
  <author>opencart2x.ru</author>
  <link>https://opencart2x.ru</link>
  <file path="admin/view/template/customer/custom_field_list.tpl">
    <operation>
      <search><![CDATA[<td class="text-right"><?php if ($sort == 'cf.sort_order') { ?>]]></search>
      <add position="before"><![CDATA[<td style="width: 1px;" class="text-center">Шорткод</td>]]></add>
    </operation>
    <operation>
      <search><![CDATA[<td class="text-right"><?php echo $custom_field['sort_order']; ?></td>]]></search>
      <add position="before"><![CDATA[<td class="text-center">{custom_field_<?php echo $custom_field['custom_field_id']; ?>}</td>]]></add>
    </operation>
    <operation>
      <search><![CDATA[<td class="text-center" colspan="6"><?php echo $text_no_results; ?></td>]]></search>
      <add position="replace"><![CDATA[<td class="text-center" colspan="7"><?php echo $text_no_results; ?></td>]]></add>
    </operation>
  </file>
  <file path="admin/model/localisation/country.php">
    <operation>
      <search><![CDATA[$this->db->query("UPDATE " . DB_PREFIX . "country SET name = '" . $this->db->escape($data['name']) . "', iso_code_2 = '" . $this->db->escape($data['iso_code_2']) . "', iso_code_3 = '" . $this->db->escape($data['iso_code_3']) . "', address_format = '" . $this->db->escape($data['address_format']) . "', postcode_required = '" . (int)$data['postcode_required'] . "', status = '" . (int)$data['status'] . "' WHERE country_id = '" . (int)$country_id . "'");]]></search>
      <add position="after"><![CDATA[$this->db->query("UPDATE " . DB_PREFIX . "order SET shipping_address_format = '" . $this->db->escape($data['address_format']) . "', payment_address_format = '" . $this->db->escape($data['address_format']) . "'");]]></add>
    </operation>
  </file>
  <file path="admin/controller/sale/order.php">
    <operation>
      <search><![CDATA[$data['payment_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="after"><![CDATA[$data['payment_address'] = ($order_info['payment_firstname']) ? $data['payment_address'] : '';]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['payment_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      $this->load->model('customer/custom_field');
      $custom_fields = $this->model_customer_custom_field->getCustomFields();

      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($payment_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  $custom_field_options = $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']);

                  foreach ($custom_field_options as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $payment_custom_field_value) {
                          $payment_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $payment_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['shipping_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="after"><![CDATA[$data['shipping_address'] = ($order_info['shipping_firstname']) ? $data['shipping_address'] : '';]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['shipping_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['shipping_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($shipping_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  $custom_field_options = $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']);

                  foreach ($custom_field_options as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $shipping_custom_field_value) {
                          $shipping_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $shipping_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);]]></search>
      <add position="replace"><![CDATA[$custom_fields = $this->model_customer_custom_field->getCustomFields(array('sort' => 'cf.sort_order'));]]></add>
    </operation>
    <operation>
      <search><![CDATA[foreach ($orders as $order_id) {]]></search>
      <add position="before"><![CDATA[
      $this->load->model('customer/custom_field');
      $custom_fields = $this->model_customer_custom_field->getCustomFields();
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$payment_address = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="after"><![CDATA[$payment_address = ($order_info['payment_firstname']) ? $payment_address : '';]]></add>
    </operation>
    <operation>
      <search><![CDATA[$payment_address = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($payment_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  $custom_field_options = $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']);

                  foreach ($custom_field_options as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $payment_custom_field_value) {
                          $payment_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $payment_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$shipping_address = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="after"><![CDATA[$shipping_address = ($order_info['shipping_firstname']) ? $shipping_address : '';]]></add>
    </operation>
    <operation>
      <search><![CDATA[$shipping_address = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['shipping_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($shipping_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  $custom_field_options = $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']);

                  foreach ($custom_field_options as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $shipping_custom_field_value) {
                          $shipping_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $shipping_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
  </file>
  <file path="catalog/model/account/order.php">
    <operation>
      <search><![CDATA['payment_address_format'  => $order_query->row['payment_address_format'],]]></search>
      <add position="after"><![CDATA[
      'custom_field' => json_decode($order_query->row['custom_field'], true),
      'payment_custom_field' => json_decode($order_query->row['payment_custom_field'], true),
      'shipping_custom_field' => json_decode($order_query->row['shipping_custom_field'], true),
      ]]></add>
    </operation>
  </file>
  <file path="catalog/controller/account/address.php">
    <operation>
      <search><![CDATA[foreach ($results as $result) {]]></search>
      <add position="before"><![CDATA[
      $this->load->model('account/custom_field');
      $account_custom_fields = $this->model_account_custom_field->getCustomFields();
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$find = array(]]></search>
      <add position="before"><![CDATA[
      foreach ($result['custom_field'] as $key => $custom_field) {
          $account_custom_field_value = $custom_field;

          if (isset($account_custom_fields[$key]['type'])) {
              if ($account_custom_fields[$key]['type'] == 'select' || $account_custom_fields[$key]['type'] == 'radio' || $account_custom_fields[$key]['type'] == 'checkbox') {
                  foreach ($account_custom_fields[$key]['custom_field_value'] as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $account_custom_field_value) {
                          $account_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }
          }

          $format = str_replace('{custom_field_' . $key . '}', $account_custom_field_value, $format);
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
  </file>
  <file path="catalog/controller/account/order.php">
    <operation>
      <search><![CDATA[if ($order_info['payment_address_format']) {]]></search>
      <add position="before"><![CDATA[
      $this->load->model('account/custom_field');
      $custom_fields = $this->model_account_custom_field->getCustomFields();
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['payment_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($payment_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  foreach ($custom_field['custom_field_value'] as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $payment_custom_field_value) {
                          $payment_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $payment_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['shipping_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['shipping_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($shipping_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  foreach ($custom_field['custom_field_value'] as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $shipping_custom_field_value) {
                          $shipping_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $shipping_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
  </file>
  <file path="catalog/model/checkout/order.php">
    <operation>
      <search><![CDATA[if ($order_info) {]]></search>
      <add position="after"><![CDATA[
      $this->load->model('account/custom_field');
      $custom_fields = $this->model_account_custom_field->getCustomFields();
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['payment_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $payment_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($payment_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  foreach ($custom_field['custom_field_value'] as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $payment_custom_field_value) {
                          $payment_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $payment_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['shipping_address'] = str_replace(array("
", "
", "
"), '<br />', preg_replace(array("/\s\s+/", "/

+/", "/

+/"), '<br />', trim(str_replace($find, $replace, $format))));]]></search>
      <add position="before"><![CDATA[
      foreach ($custom_fields as $custom_field) {
          if (isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['shipping_custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
              $shipping_custom_field_value = $order_info['custom_field'][$custom_field['custom_field_id']];
          }

          if (isset($shipping_custom_field_value)) {
              if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio' || $custom_field['type'] == 'checkbox') {
                  foreach ($custom_field['custom_field_value'] as $custom_field_option) {
                      if ($custom_field_option['custom_field_value_id'] == $shipping_custom_field_value) {
                          $shipping_custom_field_value = $custom_field_option['name'];
                      }
                  }
              }

              $format = str_replace('{custom_field_' . $custom_field['custom_field_id'] . '}', $shipping_custom_field_value, $format);
          }
      }

      $format = preg_replace('/(({custom_field(.*?)?}))/sim', '', $format);
      ]]></add>
    </operation>
  </file>
</modification>