You are here

function ip2country_form_uc_cart_checkout_form_alter in IP-based Determination of a Visitor's Country 8

Same name and namespace in other branches
  1. 6 ip2country.module \ip2country_form_uc_cart_checkout_form_alter()
  2. 7 ip2country.module \ip2country_form_uc_cart_checkout_form_alter()

Implements hook_form_FORM_ID_alter().

Alters Ubercart's uc_cart_checkout_form() to use ip2country's country determination as the default billing and delivery country. If the user's country hasn't been determined, the store country will be used instead.

File

./ip2country.module, line 180
Determination of user's Country based on IP.

Code

function ip2country_form_uc_cart_checkout_form_alter(&$form, &$form_state) {
  global $user;
  if (isset($user->data['country_iso_code_2'])) {
    if (\Drupal::moduleHandler()
      ->moduleExists('uc_store')) {
      $connection = \Drupal::database();
      $country_id = $connection
        ->query('SELECT country_id from {uc_countries} WHERE country_iso_code_2 = :iso2', [
        ':iso2' => $user->data['country_iso_code_2'],
      ])
        ->fetchField();
      if (!$country_id) {
        $country_id = uc_store_default_country();
      }
      $form['panes']['billing']['billing_country']['#default_value'] = $form['panes']['delivery']['delivery_country']['#default_value'] = $country_id;
    }
  }
}