Drupal 8 Commerce get country code by country, state code by state

/**
 * Helper function.
 */
function _mba_salesforce_country_code_to_country($country_code) {
  $full_country_list = \Drupal::service('country_manager')->getList();
  if (!empty($full_country_list[$country_code])) {
    return $full_country_list[$country_code]->render();
  }
  else {
    return '';
  }
}

/**
 * Helper function.
 */
function _mba_salesforce_country_to_country_code($country) {
  $country = strtolower($country);
  $full_country_list = \Drupal::service('country_manager')->getList();
  $country_list = [];
  foreach ($full_country_list as $country_code => $country_name) {
    $country_list[$country_code] = strtolower($country_name->render());
  }
  if (array_search($country, $country_list)) {
    return array_search($country, $country_list);
  }
  else {
    return '';
  }
}

/**
 * Helper function.
 */
function _mba_salesforce_state_code_to_state($state_code) {
  $full_state_list = \Drupal::service('address.subdivision_repository')->getList([$country_code]);
  if (!empty($full_state_list[$state_code])) {
    return $full_state_list[$state_code];
  }
  else {
    return '';
  }
}

/**
 * Helper function.
 *
 * @todo Add validation for States aliases.
 */
function _mba_salesforce_state_to_state_code($state, $country) {
  $state = trim($state);
  $state = strtolower($state);
  $country_code = _mba_salesforce_country_to_country_code($country);
  $full_state_list = \Drupal::service('address.subdivision_repository')->getList([$country_code]);
  $state_list = [];
  foreach ($full_state_list as $state_code => $state_name) {
    $state_list[$state_code] = strtolower($state_name);
  }
  if (array_search($state, $state_list)) {
    return array_search($state, $state_list);
  }
  else {
    return '';
  }
}

 

Комментарии

Добавить комментарий

Войти, используя Loginza Google Account Yandex Mail.ru Vkontakte Facebook

Plain text

  • HTML-теги не обрабатываются и показываются как обычный текст
  • Адреса страниц и электронной почты автоматически преобразуются в ссылки.
  • Строки и параграфы переносятся автоматически.

У моего очень близкого друга

У моего очень близкого друга скоро День рождения, и я не знаю, чего ему преподнести. Дело в том, что он любит красивые шахматы
шахматы как выбрать? прошу пособничество в выборе [url=https://chess-historical.ru]Здесь[/url]

У моего любимого скоро юбилей

У моего любимого скоро юбилей, и я не могу понять, чего ему преподнести. Дело в том, что он любит играть в шахматы
как выбрать шахматы на подарок? прошу помощи в выборе [url=https://chess-historical.ru]сайт[/url]