How to add your list of countries on checkout page?

Let's say I have a list of countries and regions: https://codepen.io/Korotenko/pen/JjoGXVQ

There is a class
plugins/woocommerce/includes/class-wc-countries.php


There is such code:
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 ) . '&mdash; ' . ( $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.
}
}
}
 }


Can I somehow add your list of countries ?
April 3rd 20 at 17:20
1 answer
April 3rd 20 at 17:22
Well, in the first search link. Tyk.
I want to add that you can add the empty plugin
Either through woody snippet - rebeka commented on April 3rd 20 at 17:25
I saw this option, but you my example looked ?
https://codepen.io/Korotenko/pen/JjoGXVQ

I get json countries: https://raw.githubusercontent.com/Genri-Rus/Json/m...
And the list of regions, respectively: https://raw.githubusercontent.com/Genri-Rus/Json/m...

There is a layout:

the <div>
 <select name="countryes" id="countryes"></select>
</div>

the <div>
 <select name="regions" id="regions"></select>
</div>


I need this layout to json'you could take and how to do it ?

To insert the layout in the filter ?
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
new_rus_woocommerce_states function( $states ) {
$states[] = array(
"............"
);

return $states;
}
- larue_Zemlak83 commented on April 3rd 20 at 17:28
@rebeka, what do you think it is better to get the data json's ? - larue_Zemlak83 commented on April 3rd 20 at 17:31
There are generally ideas how to implement it with json ?
Or still I have to manually add ? - larue_Zemlak83 commented on April 3rd 20 at 17:34
@larue_Zemlak83, well, what's the problem to get this json via php and then expand the regions and countries to add as in the example above? The logic will be almost identical to how it works using jquery, only the output you will have a multidimensional array, which you'll need to add similar instructions on my link.

If you need the user chose region duhsasana, as in the example, do the same thing, only real-dropdown hide and build upon him a visual wrapper for jquery, which will work almost the same way as your example on codepen.

The key point is that to work correctly you need WooCommerce, so he knew about all the regions of delivery. If you dynamically pull from the front, it is not. - Cristobal.Johns commented on April 3rd 20 at 17:37
@Cristobal.Johns, thanks for the answer, and you could show the first embodiment is a small example of how this can be done ?

Well, I get json countries: https://raw.githubusercontent.com/Genri-Rus/Json/m...
And then json regions: https://raw.githubusercontent.com/Genri-Rus/Json/m... - larue_Zemlak83 commented on April 3rd 20 at 17:40
@larue_Zemlak83, the principle of here - https://stackoverflow.com/questions/15617512/get-j...
How to pass arrays in a loop, I hope to paint it is not necessary? - alessandra commented on April 3rd 20 at 17:43
@alessandra, thank you, but in the example here: https://anfisabreus.ru/2017/08/kak-dobavit-goroda-...

in the filter add region codes, but if I have an array then how ?

I have to write codes manually ? - larue_Zemlak83 commented on April 3rd 20 at 17:46
@alessandra,

Inside the loop need to run?
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
new_rus_woocommerce_states function( $states ) {
$states[] = array(
"............"
);

return $states;
}
- larue_Zemlak83 commented on April 3rd 20 at 17:49
@alessandra, maybe you saw an example on stackoverflow how to add this method for woocommerce ?

I was looking for, but anything similar was not able to find - larue_Zemlak83 commented on April 3rd 20 at 17:52
@larue_Zemlak83, soran, accidentally found a second account.

By sabzh: it is not necessary to obtain the data in the filter. Once you need to pass arrays:
  • parsite of the country, they have ranges. The result is a one-dimensional array, where one country = 1 array element
  • similarly parsite regions
  • pass the array of countries within each run of the loop regions and to shift the resulting array to all regions that have a parent -
  • using print_r prints the array and put its contents in the file, then connect it as in the example on the first link
- Cristobal.Johns commented on April 3rd 20 at 17:55
PS Before adding their countries/regions I'd kill default ucommerce. - Cristobal.Johns commented on April 3rd 20 at 17:58
@Cristobal.Johns, right ?

$url = "https://raw.githubusercontent.com/Genri-Rus/Json/master/countries.json";
$json = file_get_contents($url);
$json_data = json_decode($json, true);

Then run a foreach loop and inside it to the regions ?
using print_r prints the array and put its contents in the file, then connect it as in the example on the first link

add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
new_rus_woocommerce_states function( $states ) {
$states[] = array(
 "here to attach the file ?"
);

return $states;
}
- larue_Zemlak83 commented on April 3rd 20 at 18:01
@Cristobal.Johns, am I doing it right ? - larue_Zemlak83 commented on April 3rd 20 at 18:04
@larue_Zemlak83, something like that. - Cristobal.Johns commented on April 3rd 20 at 18:07
@Cristobal.Johns, and you could read more show how to connect the arrays of countries, regions ?

And then this array to a file to put ?

And I also wanted to clarify, the output I get the same thing: https://codepen.io/Korotenko/pen/JjoGXVQ

I need when choosing a country to load only the regions of the selected country - larue_Zemlak83 commented on April 3rd 20 at 18:10
@larue_Zemlak83, no offense, but to write you all the code and plug it I won't: no time. - Cristobal.Johns commented on April 3rd 20 at 18:13
@Cristobal.Johns, and that there is a lot of writing ?

You're kind of an example on stackoverflow led ?

And with the array, it is necessary to create an array and connect the array of countries array of regions

And you have not answered the Question:

And I also wanted to clarify, the output I get the same thing: https://codepen.io/Korotenko/pen/JjoGXVQ
I need when choosing a country to load only the regions of the selected country
- larue_Zemlak83 commented on April 3rd 20 at 18:16
@larue_Zemlak83, I already gave enough information in order for you do simple step by step written code.

By region - Yes, will boot the selected region. - Cristobal.Johns commented on April 3rd 20 at 18:19
@Cristobal.Johns, thank you

And you yourself did a similar option or use default ? - larue_Zemlak83 commented on April 3rd 20 at 18:22
@larue_Zemlak83, used by default, I always lacked.

Poked with a stick WC, there is a need to define the array of countries separately: https://wp-kama.ru/hook/woocommerce_countries ie first parson of the country, put them in an array like this:

parseJsonToArray function( $url ){
 return json_decode( file_get_contents( $url ), true);
}

makeTextIndex function( $string ) {
 return strtoupper(
preg_replace(
'/[^a-zA-Z0-9_-]/',
",
 str_replace(" ", "_", $string )
)
 ); //here it is possible to do the cropping, but even in the case of the first 4 symbols of Latin names obtained several takes
}

$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'];
}


The variable $result_countries put through the filter instead of the standard countries, will understand how.

Then shift the regions similar and put as the link above. All in all dismissive. - Cristobal.Johns commented on April 3rd 20 at 18:25
@Cristobal.Johns, thank you very much

I'm sorry, but why do you refer to
$country_text_index = makeTextIndex( $country['title_en'] )
?

I just title_en not needed and when you click on your link I got error: This page does not exist \_(ツ)_/ - larue_Zemlak83 commented on April 3rd 20 at 18:28
I'm sorry, but why do you refer to
$country_text_index = makeTextIndex( $country['title_en'] )?
I just title_en not needed

Um, to make a text index of the country.
The reference should have been to remove the point. - Cristobal.Johns commented on April 3rd 20 at 18:31
@Cristobal.Johns, and it can be done ?
$result_countries[ makeTextIndex( $country['title_ru'] ] = $country['title_ru'];
- larue_Zemlak83 commented on April 3rd 20 at 18:34
@Cristobal.Johns, I'm sorry, right I do ?

parseJsonToArray function( $url ){
 return json_decode( file_get_contents( $url ), true);
}

makeTextIndex function( $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'];
}


Furthermore, for those countries I did
add_filter( 'woocommerce_countries', 'filter_function_name_6672' );
filter_function_name_6672 function( $include ){
 $include = ";
 foreach ($result_countries as $key => $result_country) {
 $include[$key] = [ $result_country ];
}

 return include;
}

For regions like this:
add_filter( 'woocommerce_states', 'new_rus_woocommerce_states' );
new_rus_woocommerce_states function( $states ) {
 foreach ($result_regions as $key => $result_region) {
 $states[$key] = [ $result_region ];
}
 return $states;
}


Right I'm doing and now how to associate countries with regions ?
I have a country_id, its in the array you also need to add ? Advance would be grateful - larue_Zemlak83 commented on April 3rd 20 at 18:37
@Cristobal.Johns, I have a country:
5e14bc8b86b76112841795.png
But now flew in the admin the choice of delivery:
5e14bcdaf0398921610570.png - larue_Zemlak83 commented on April 3rd 20 at 18:40

Find more questions by tags WordPressWooCommerce