null, 'selling_mode' => 'all', 'selling_countries' => array(), 'shipping_mode' => 'all', 'shipping_everywhere' => false, 'shipping_countries' => array(), 'currency' => function_exists( 'get_woocommerce_currency' ) ? (string) get_woocommerce_currency() : null, 'locale' => $locale, 'language' => strtolower( (string) strtok( $locale, '_-' ) ), ); if ( ! Util::woo_active() || ! function_exists( 'WC' ) || ! WC()->countries ) { return $out; } $c = WC()->countries; $all = array_keys( (array) $c->get_countries() ); $out['base_country'] = (string) $c->get_base_country(); // Selling: woocommerce_allowed_countries = all | all_except | specific. $mode = (string) get_option( 'woocommerce_allowed_countries', 'all' ); $out['selling_mode'] = in_array( $mode, array( 'all', 'all_except', 'specific' ), true ) ? $mode : 'all'; $selling = array_keys( (array) $c->get_allowed_countries() ); $out['selling_countries'] = self::codes( $selling ); // Ship-to: woocommerce_ship_to_countries = '' (same as selling) | all | specific | disabled. $ship_mode = (string) get_option( 'woocommerce_ship_to_countries', '' ); $out['shipping_mode'] = '' === $ship_mode ? 'selling' : ( in_array( $ship_mode, array( 'all', 'specific', 'disabled' ), true ) ? $ship_mode : 'selling' ); if ( 'disabled' === $ship_mode ) { return $out; } $ship_to = array_keys( (array) $c->get_shipping_countries() ); // Shipping zones: only zones with at least one enabled method count. $zone_countries = array(); $everywhere = false; if ( class_exists( '\WC_Shipping_Zones' ) ) { $continents = (array) $c->get_continents(); foreach ( (array) \WC_Shipping_Zones::get_zones() as $z ) { if ( ! self::has_enabled_method( (array) ( $z['shipping_methods'] ?? array() ) ) ) { continue; } foreach ( (array) ( $z['zone_locations'] ?? array() ) as $loc ) { $type = is_object( $loc ) ? (string) $loc->type : ''; $code = is_object( $loc ) ? (string) $loc->code : ''; if ( 'country' === $type ) { $zone_countries[] = $code; } elseif ( 'state' === $type ) { $zone_countries[] = (string) strtok( $code, ':' ); } elseif ( 'continent' === $type && isset( $continents[ $code ]['countries'] ) ) { $zone_countries = array_merge( $zone_countries, (array) $continents[ $code ]['countries'] ); } } } // Zone 0 ("Locations not covered by your other zones") with an enabled method ships everywhere allowed. $rest = \WC_Shipping_Zones::get_zone( 0 ); if ( $rest && self::has_enabled_method( (array) $rest->get_shipping_methods( true ) ) ) { $everywhere = true; } } if ( $everywhere ) { $out['shipping_everywhere'] = count( array_diff( $all, $ship_to ) ) === 0; $out['shipping_countries'] = self::codes( $ship_to ); } else { $out['shipping_countries'] = self::codes( array_intersect( $zone_countries, $ship_to ) ); } return $out; } private static function has_enabled_method( array $methods ): bool { foreach ( $methods as $m ) { if ( is_object( $m ) && isset( $m->enabled ) && 'yes' === $m->enabled ) { return true; } } return false; } /** Unique, sorted ISO 3166-1 alpha-2 codes. */ private static function codes( array $codes ): array { $codes = array_filter( array_map( 'strval', $codes ), static fn( $x ) => (bool) preg_match( '/^[A-Z]{2}$/', $x ) ); $codes = array_values( array_unique( $codes ) ); sort( $codes ); return $codes; } }