• Resolved mohammedmalleck

    (@mohammedmalleck)


    Hi , so I was looking at the debug.php file & I realised that I have some Shipping class zones errors which I cannot how to *fix*.
    The errors are these
    Undefined array key “MK/ST/KS/GB-NIR” in /wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php on line 125

    FYI , I am using the below code in funcitions.php to make sure only the countries I allow are used in places where woocomerce adds countries , ps:. the priority is to have control over which countries are shown in countries field in woocomerce checkout page.

    // Filter allowed countries
    add_filter('woocommerce_countries', 'filter_allowed_countries');
    function filter_allowed_countries($countries) {
    $allowed_countries = array(
    'AE' => 'United Arab Emirates',
    'OM' => 'Oman',
    'PK' => 'Pakistan',
    'QA' => 'Qatar',
    'BH' => 'Bahrain',
    'BD' => 'Bangladesh',
    'SA' => 'Saudi Arabia',
    'LK' => 'Sri Lanka',
    'IN' => 'India',
    'KW' => 'Kuwait',
    'LB' => 'Lebanon',
    'LY' => 'Libya',
    'AT' => 'Austria',
    'AU' => 'Australia',
    'AZ' => 'Azerbaijan',
    'FR' => 'France',
    'GE' => 'Georgia',
    'DE' => 'Germany',
    'MC' => 'Monaco',
    'MY' => 'Malaysia',
    'MO' => 'Macao',
    'LI' => 'Liechtenstein',
    'CY' => 'Cyprus',
    'DK' => 'Denmark',
    'KZ' => 'Kazakhstan',
    'JO' => 'Jordan',
    'IT' => 'Italy',
    'NL' => 'Netherlands',
    'PH' => 'Philippines',
    'PT' => 'Portugal',
    'CH' => 'Switzerland',
    'TW' => 'Taiwan',
    'TH' => 'Thailand',
    'UZ' => 'Uzbekistan',
    'VA' => 'Vatican',
    'BE' => 'Belgium',
    'EG' => 'Egypt',
    'GR' => 'Greece',
    'SG' => 'Singapore',
    'ES' => 'Spain',
    'BN' => 'Brunei',
    'HK' => 'Hong Kong',
    'ID' => 'Indonesia',
    'KR' => 'South Korea',
    'NP' => 'Nepal',
    'NG' => 'Nigeria',
    'PL' => 'Poland',
    'SE' => 'Sweden',
    'TR' => 'Turkey',
    'US' => 'United States',
    'ET' => 'Ethiopia',
    'GH' => 'Ghana',
    'GA' => 'Gabon',
    'MG' => 'Madagascar',
    'MT' => 'Malta',
    'MU' => 'Mauritius',
    'MZ' => 'Mozambique',
    'RW' => 'Rwanda',
    'SO' => 'Somalia',
    'SD' => 'Sudan',
    'CN' => 'China',
    'KE' => 'Kenya',
    'PS' => 'Palestinian Territory',
    'TZ' => 'Tanzania',
    'TN' => 'Tunisia',
    'UG' => 'Uganda',
    'DJ' => 'Djibouti',
    'LS' => 'Lesotho',
    'ZM' => 'Zambia',
    'ZW' => 'Zimbabwe',
    'AM' => 'Armenia',
    'BJ' => 'Benin',
    'LU' => 'Luxembourg',
    'MW' => 'Malawi',
    'ML' => 'Mali',
    'MA' => 'Morocco',
    'SL' => 'Sierra Leone',
    'ZA' => 'South Africa',
    'BF' => 'Burkina Faso',
    'CA' => 'Canada',
    'CG' => 'Congo (Brazzaville)',
    'CZ' => 'Czech Republic',
    'GN' => 'Guinea',
    'KG' => 'Kyrgyzstan',
    'NA' => 'Namibia',
    'NZ' => 'New Zealand',
    'TG' => 'Togo',
    'UA' => 'Ukraine',
    'VN' => 'Vietnam',
    'LR' => 'Liberia',
    'RU' => 'Russia',
    'SY' => 'Syria',
    'AL' => 'Albania',
    'AD' => 'Andorra',
    'AO' => 'Angola',
    'EE' => 'Estonia',
    'FO' => 'Faroe Islands',
    'GI' => 'Gibraltar',
    'GL' => 'Greenland',
    'LT' => 'Lithuania',
    'MV' => 'Maldives',
    'MD' => 'Moldova',
    'SM' => 'San Marino',
    'SN' => 'Senegal',
    'SC' => 'Seychelles',
    'SK' => 'Slovakia',
    'SI' => 'Slovenia',
    'BA' => 'Bosnia and Herzegovina',
    'BW' => 'Botswana',
    'BI' => 'Burundi',
    'KH' => 'Cambodia',
    'HR' => 'Croatia',
    'HU' => 'Hungary',
    'IQ' => 'Iraq',
    'IE' => 'Ireland',
    'IM' => 'Isle of Man',
    'CI' => 'Ivory Coast',
    'JP' => 'Japan',
    'LA' => 'Laos',
    'LV' => 'Latvia',
    'MM' => 'Myanmar',
    'SZ' => 'Eswatini',
    'TJ' => 'Tajikistan',
    'TM' => 'Turkmenistan',
    'YE' => 'Yemen',
    'DZ' => 'Algeria',
    'AS' => 'American Samoa',
    'AI' => 'Anguilla',
    'AG' => 'Antigua and Barbuda',
    'AR' => 'Argentina',
    'AW' => 'Aruba',
    'BS' => 'Bahamas',
    'BB' => 'Barbados',
    'BY' => 'Belarus',
    'BZ' => 'Belize',
    'BM' => 'Bermuda',
    'BT' => 'Bhutan',
    'BO' => 'Bolivia',
    'TL' => 'Timor-Leste',
    'EC' => 'Ecuador',
    'SV' => 'El Salvador',
    'GQ' => 'Equatorial Guinea',
    'FK' => 'Falkland Islands',
    'FJ' => 'Fiji',
    'FI' => 'Finland',
    'GF' => 'French Guiana',
    'PF' => 'French Polynesia',
    'GM' => 'Gambia',
    'GD' => 'Grenada',
    'GP' => 'Guadeloupe',
    'MH' => 'Marshall Islands',
    'MQ' => 'Martinique',
    'MR' => 'Mauritania',
    'YT' => 'Mayotte',
    'MX' => 'Mexico',
    'MN' => 'Mongolia',
    'ME' => 'Montenegro',
    'MS' => 'Montserrat',
    'WS' => 'Samoa',
    'RS' => 'Serbia',
    'SB' => 'Solomon Islands',
    'PM' => 'Saint Pierre and Miquelon',
    'KN' => 'Saint Kitts and Nevis',
    'LC' => 'Saint Lucia',
    'VC' => 'Saint Vincent and the Grenadines',
    'SR' => 'Suriname',
    'SJ' => 'Svalbard and Jan Mayen',
    'BR' => 'Brazil',
    'BG' => 'Bulgaria',
    'CV' => 'Cape Verde',
    'KY' => 'Cayman Islands',
    'CF' => 'Central African Republic',
    'CL' => 'Chile',
    'CX' => 'Christmas Island',
    'CC' => 'Cocos (Keeling) Islands',
    'CO' => 'Colombia',
    'KM' => 'Comoros',
    'CK' => 'Cook Islands',
    'CR' => 'Costa Rica',
    'CU' => 'Cuba',
    'GU' => 'Guam',
    'GT' => 'Guatemala',
    'GW' => 'Guinea-Bissau',
    'GY' => 'Guyana',
    'HT' => 'Haiti',
    'HN' => 'Honduras',
    'IS' => 'Iceland',
    'JM' => 'Jamaica',
    'KI' => 'Kiribati',
    'NR' => 'Nauru',
    'NC' => 'New Caledonia',
    'NI' => 'Nicaragua',
    'NE' => 'Niger',
    'NF' => 'Norfolk Island',
    'MP' => 'Northern Mariana Islands',
    'NO' => 'Norway',
    'PW' => 'Belau',
    'PA' => 'Panama',
    'PG' => 'Papua New Guinea',
    'PY' => 'Paraguay',
    'PE' => 'Peru',
    'PN' => 'Pitcairn',
    'PR' => 'Puerto Rico',
    'TO' => 'Tonga',
    'TT' => 'Trinidad and Tobago',
    'TC' => 'Turks and Caicos Islands',
    'TV' => 'Tuvalu',
    'UY' => 'Uruguay',
    'VU' => 'Vanuatu',
    'VE' => 'Venezuela',
    'VG' => 'Virgin Islands (British)',
    'VI' => 'Virgin Islands (US)',
    'EH' => 'Western Sahara',
    'DM' => 'Dominica',
    'DO' => 'Dominican Republic',
    'RE' => 'Reunion',
    'RO' => 'Romania',
    'GB' => 'United Kingdom', // Added United Kingdom
    'GB-NIR' => 'Northern Ireland'
    );

    // Filter the countries array to only include allowed countries
    return array_intersect_key($countries, $allowed_countries);
    }

    Maybe something in the above code is causing the issue but I am not sure what is , and how to solve it…
    Will appreciate any help. Ty!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Mahfuzur Rahman(woo-hc)

    (@mahfuzurwp)

    Hi @mohammedmalleck,

    The issue is likely caused by the custom filter_allowed_countries function, which filters WooCommerce countries. WooCommerce uses specific codes for regions, such as “GB-NIR” for Northern Ireland, in shipping zones. If these region codes are not properly included in the $allowed_countries array returned by your filter, it results in the “Undefined array key” error. To resolve this, ensure all region codes used by WooCommerce, like “GB-NIR,” are correctly defined in your array.

    As this involves custom code, I understand it can feel a bit tricky to navigate. I recommend reaching out to a developer who can carefully review and adjust your code to ensure it works seamlessly with WooCommerce.

    Thank you.

    Thread Starter mohammedmalleck

    (@mohammedmalleck)

    Hi @mahfuzurwp ,
    So I added these countires in the list

           'GB-NIR' => 'Northern Ireland',
    'ST' => 'Scotland',
    'MK' => 'Macedonia',
    'KS' => 'Kansas'

    Now I only get error for ‘GB-NIR’ & ‘KS’ in debug.log file. File error below :

    [12-Jan-2025 12:50:04 UTC] PHP Warning:  Undefined array key "GB-NIR" in /wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php on line 125

    [12-Jan-2025 12:50:04 UTC] PHP Warning: Undefined array key "KS" in /wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php on line 125

    I am aware of code to a good extent , I would just like some support from the woocomerce support team on how I can modify my code such that it does not cause errors with woocomerce pre-written code. Let me know if that help is possible. Ty!

    Plugin Support Moses M. (woo-hc)

    (@mosesmedh)

    Hi @mohammedmalleck,

    I’m glad to hear you’re getting the hang of it. While we’re happy to assist with core WooCommerce issues on this forum, support for custom code and customization falls outside our scope.

    You can join the WooCommerce community channel for further help or consider hiring a developer to assist with this.

    Saif

    (@babylon1999)

    Hello @mohammedmalleck!

    The errors are these
    Undefined array key “MK/ST/KS/GB-NIR” in /wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php on line 125

    This is expected because you’re introducing countries that WooCommerce doesn’t recognize.

    First, to change the list of countries that appear in the checkout, you don’t need a snippet at all. Simply navigate to WooCommerce -> Settings -> General -> Sell to specific countries and select those countries from there.

    As for adding your own countries, such as Northern Ireland and Scotland, you need to append your array to the existing one. Here’s an example:

    /**
    * Add a new country to countries list
    */
    add_filter( 'woocommerce_countries', 'handsome_bearded_guy_add_my_country' );
    function handsome_bearded_guy_add_my_country( $countries ) {
    $new_countries = array(
    'KS' => __( 'Kansas', 'woocommerce' ),
    'GB-NIR' => __( 'Northern Ireland', 'woocommerce' ),
    'ST' => __( 'Scotland', 'woocommerce' ),
    'MK' => __( 'Macedonia', 'woocommerce' ),
    );
    return array_merge( $countries, $new_countries );
    }

    You should also assign these countries a content so that you can select them in the shipping settings.?

    add_filter( 'woocommerce_continents', 'handsome_bearded_guy_add_my_country_to_continents' );
    function handsome_bearded_guy_add_my_country_to_continents( $continents ) {
    $continents['EU']['countries'][] = 'GB-NIR';
    $continents['EU']['countries'][] = 'ST';
    $continents['EU']['countries'][] = 'MK';
    $continents['NA']['countries'][] = 'KS';
    return $continents;
    }

    Cheers!

    Thread Starter mohammedmalleck

    (@mohammedmalleck)

    Hi @babylon1999 !,
    Thanks for the help , but here’s the thing. I have a lot of countries to sell to & adding them manually via the WooCommerce -> Settings -> General -> Sell to specific countries sounds too long to me..
    from what I was able to deduce some of the countries WooCommerce sells to is not where I want to sell hence selecting all countries is not the solution also.

    So I decided to go a step ahead and only allow the countries I want to sell to and then select all countries in WooCommerce. However it is not proving to be ideal as of now.

    What would be your take to solve this?

    Plugin Support Moses M. (woo-hc)

    (@mosesmedh)

    Hi @mohammedmalleck,

    Additionally, there’s an option to sell to all countries except for specific ones. This allows you to exclude the countries you don’t wish to sell to. You can check if this option suits your needs. If not, you may want to consider getting professional help or hiring certified WooCommerce developers from here: https://woocommerce.com/development-services/

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.