martin2020 Posted September 28, 2020 Share Posted September 28, 2020 Hi all Got a problem that seems simple, but I fail to solve it. Anybody who can help with country array? Right now the following code is active, which limits the available country to US only, for a certain language/area selection. It works fine. I would now only like to include an additional country (CA) as a choice besides US for the same language/area selection (without changing the language/area selection). If anyone knows the trick pls help. Existing Code: if( !function_exists('filter_woocommerce_countries_shipping_countries') ) { function filter_woocommerce_countries_shipping_countries( $countries ) { // Filter countries for language if( function_exists('icl_get_languages') ) { $current_lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : 'gl_en'; if( isset( $countries['US'] ) ) { if( $current_lang == 'en' ) { $countries = array( 'US' => $countries['US'] ); } else { unset( $countries['US'] ); } } } return $countries; } } Quote Link to comment https://forums.phpfreaks.com/topic/311535-country-array/ Share on other sites More sharing options...
martin2020 Posted September 28, 2020 Author Share Posted September 28, 2020 discovered the solution: $countries = array( 'US' => $countries['US'], 'CA' => $countries['CA'] ); Quote Link to comment https://forums.phpfreaks.com/topic/311535-country-array/#findComment-1581613 Share on other sites More sharing options...
gizmola Posted October 3, 2020 Share Posted October 3, 2020 Another way of doing this that uses some array functions would be: $countries = array_intersect_key($countries, array_fill_keys(array('US','CA'),0)); Quote Link to comment https://forums.phpfreaks.com/topic/311535-country-array/#findComment-1581725 Share on other sites More sharing options...
NotionCommotion Posted October 4, 2020 Share Posted October 4, 2020 (edited) I would do it this way $countries = array_intersect_key($countries, ['US'=>null, 'CA'=>null]); unless I wanted to be super tricky in which I would do it this way $countries = array_intersect_key($countries, array_flip(['US', 'CA'])); gizmola's way is defiantly more tricky than mine. Edited October 4, 2020 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/311535-country-array/#findComment-1581733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.