each( function ( $settings, $code ) use ( &$currency ) { if ( self::isCurrencyAvailableForCountry( $settings ) ) { $currency = $code; } } ); return $currency; } /** * @return string */ public static function getUserCountry(){ if ( defined( 'WCML_GEOLOCATED_COUNTRY' ) ) { return WCML_GEOLOCATED_COUNTRY; } $allUserCountries = [ 'billing' => self::getUserCountryByAddress( 'billing' ), 'shipping' => self::getUserCountryByAddress( 'shipping' ), 'geolocation' => self::getCountryByUserIp() ]; $userCountry = $allUserCountries['billing'] ?: $allUserCountries['geolocation']; /** * This filter allows to override the address country declared by the user. * * @since 4.11.0 * * @param string $userCountry Billing address used if set otherwise geolocation country used. * @param array $allUserCountries { * @type string $billing The billing address country * @type string $shipping The shipping address country * @type string $geolocation The geolocation country * } * * @return string */ return apply_filters( 'wcml_geolocation_get_user_country', $userCountry, $allUserCountries ); } /** * Get country code from address if user logged-in. * * @param string $addressType Shipping or Billing address. * * @return string */ private static function getUserCountryByAddress( $addressType ){ $orderCountry = self::getUserCountryFromOrder( $addressType ); if( $orderCountry ){ return $orderCountry; } $current_user_id = get_current_user_id(); if ( $current_user_id ) { $customer = new \WC_Customer( $current_user_id, WC()->session ? true : false ); return 'shipping' === $addressType ? $customer->get_shipping_country() : $customer->get_billing_country(); } return ''; } /** * Get country code from order based on address. * * @param string $addressType Shipping or Billing address. * * @return string */ private static function getUserCountryFromOrder( $addressType ) { $country = ''; $wcAjax = Obj::prop( 'wc-ajax', $_GET ); if ( 'update_order_review' === $wcAjax && isset( $_POST['country'] ) ) { $country = $_POST['country']; } elseif ( 'checkout' === $wcAjax && isset( $_POST[ $addressType . '_country' ] ) ) { $country = $_POST[ $addressType . '_country' ]; } return wc_clean( wp_unslash( $country ) ); } /** * @param array $currencySettings * * @return bool */ public static function isCurrencyAvailableForCountry( $currencySettings ) { if ( isset( $currencySettings['location_mode'] ) ) { if ( 'all' === $currencySettings['location_mode'] ) { return true; } if ( 'include' === $currencySettings['location_mode'] && in_array( self::getUserCountry(), $currencySettings['countries'] ) ) { return true; } if ( 'exclude' === $currencySettings['location_mode'] && ! in_array( self::getUserCountry(), $currencySettings['countries'] ) ) { return true; } } return false; } }