You are here

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

Same name and namespace in other branches
  1. 8 ip2country.module \ip2country_form_uc_cart_checkout_form_alter()
  2. 6 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 236
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 (module_exists('uc_store')) {
      $country_id = db_query('SELECT country_id from {uc_countries} WHERE country_iso_code_2 = :iso2', array(
        ':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;
    }
  }
}