Jump to content

country array


martin2020

Recommended Posts

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; 
    }
}

 

Link to comment
Share on other sites

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 by NotionCommotion
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.