plugins/woocommerce/includes/class-wc-countries.php
public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
if ( $this->countries ) {
foreach ( $this->countries as $key => $value ) {
$states = $this->get_states( $key );
if ( $states ) {
echo '<optgroup label="' . esc_attr( $value ) . '">';
foreach ( $states as $state_key => $state_value ) {
echo '<option value="' . esc_attr( $key ) . ':' . esc_attr( $state_key ) . '"';
if ( $selected_country === $key && $selected_state === $state_key ) {
echo ' selected="selected"';
}
echo '>' . esc_html( $value ) . ' — ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; // WPCS: XSS ok.
}
echo '</optgroup>';
} else {
echo '<option';
if ( $selected_country === $key && '*' === $selected_state ) {
echo ' selected="selected"';
}
echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; // WPCS: XSS ok.
}
}
}
}
<div>
<select name="countryes" id="countryes"></select>
</div>
<div>
<select name="regions" id="regions"></select>
</div>
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
function new_rus_woocommerce_states( $states ) {
$states[] = array(
"............"
);
return $states;
}
$url = "https://raw.githubusercontent.com/Genri-Rus/Json/master/countries.json";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
через print_r распечатываете получившийся массив и кладёте его содержимое в файл, затем подключаете его как в примере по первой ссылке
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
function new_rus_woocommerce_states( $states ) {
$states[] = array(
"тут подключать файл ?"
);
return $states;
}
И еще я хотел уточнить, на выходе я получу тоже самое: https://codepen.io/Korotenko/pen/JjoGXVQ
Мне нужно при выборе страны, подгружать только регионы выбранной страны
function parseJsonToArray( $url ){
return json_decode( file_get_contents( $url ), true);
}
function makeTextIndex( $string ) {
return strtoupper(
preg_replace(
'/[^a-zA-Z0-9_-]/',
'',
str_replace(" ", "_", $string )
)
); //тут можно еще сделать обрезку, но даже в случае с 4 первыми символами латинских названий получается несколько дублей
}
$countries = parseJsonToArray('https://raw.githubusercontent.com/Genri-Rus/Json/master/countries.json');
$result = [];
foreach ($countries as $country){
$country_text_index = makeTextIndex( $country['title_en'] );
$result_countries[ $country_text_index ] = $country['title_ru'];
}
function parseJsonToArray( $url ){
return json_decode( file_get_contents( $url ), true);
}
function makeTextIndex( $string ) {
return strtoupper(
preg_replace(
'/[^a-zA-Z0-9_-]/',
'',
str_replace(" ", "_", $string )
)
);
}
$countries = parseJsonToArray('https://raw.githubusercontent.com/Genri-Rus/Json/master/countries.json');
$regions = parseJsonToArray('https://raw.githubusercontent.com/Genri-Rus/Json/master/region.json');
$result_countries = [];
$result_regions = [];
foreach ($countries as $country){
$country_text_index = makeTextIndex( $country['title_en'] );
$result_countries[ $country_text_index ] = $country['title_ru'];
}
foreach ($regions as $region){
$region_text_index = makeTextIndex( $region['title_en'] );
$result_regions[ $region_text_index ] = $region['title_ru'];
}
add_filter( 'woocommerce_countries', 'filter_function_name_6672' );
function filter_function_name_6672( $include ){
$include = '';
foreach ($result_countries as $key => $result_country) {
$include[$key] = [ $result_country ];
}
return include;
}
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
function new_rus_woocommerce_states( $states ) {
foreach ($result_regions as $key => $result_region) {
$states[$key] = [ $result_region ];
}
return $states;
}