function fetchUserCountry() { // Get country saved in user preferences. $country = eZShopFunctions::getPreferredUserCountry(); if ( !$country ) { // If not found, get country from user object // and save it to the preference. $country = eZShopFunctions::getUserCountry(); if ( $country ) eZShopFunctions::setPreferredUserCountry( $country ); } return array( 'result' => $country ); }
/** * Determine user's country. * * \public * \static */ static function getUserCountry( $user = false, $considerPreferedCountry = true ) { $requireUserCountry = eZVATManager::isUserCountryRequired(); // If current user has set his/her preferred country via the toolbar if ( $considerPreferedCountry ) { // return it $country = eZShopFunctions::getPreferredUserCountry(); if ( $country ) { eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" ); return $country; } } // Otherwise fetch country saved in the user object. if ( $user === false ) { $user = eZUser::currentUser(); } $userObject = $user->attribute( 'contentobject' ); $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry ); if ( $countryAttributeName === null ) return null; $userDataMap = $userObject->attribute( 'data_map' ); if ( !isset( $userDataMap[$countryAttributeName] ) ) { if ( $requireUserCountry ) { eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" . $userObject->attribute( 'name' ) . "' of class '" . $userObject->attribute( 'class_name' ) . "'.", __METHOD__ ); } return null; } $countryAttribute = $userDataMap[$countryAttributeName]; $countryContent = $countryAttribute->attribute( 'content' ); if ( $countryContent === null ) { if ( $requireUserCountry ) { eZDebug::writeError( "User country is not specified in object '" . $userObject->attribute( 'name' ) . "' of class '" . $userObject->attribute( 'class_name' ) . "'." , __METHOD__ ); } return null; } if ( is_object( $countryContent ) ) $country = $countryContent->attribute( 'value' ); elseif ( is_array( $countryContent ) ) { if ( is_array( $countryContent['value'] ) ) { foreach ( $countryContent['value'] as $item ) { $country = $item['Alpha2']; break; } } else { $country = $countryContent['value']; } } else { if ( $requireUserCountry ) { eZDebug::writeError( "User country is not specified or specified incorrectly in object '" . $userObject->attribute( 'name' ) . "' of class '" . $userObject->attribute( 'class_name' ) . "'." , __METHOD__ ); } return null; } return $country; }