/**
     * Set user's country.
     *
     * \public
     * \static
     */
    static function setUserCountry( $user, $country )
    {
        $userObject = $user->attribute( 'contentobject' );
        $requireUserCountry = eZVATManager::isUserCountryRequired();
        $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
        if ( $countryAttributeName === null )
        {
            return false;
        }

        $userDataMap = $userObject->attribute( 'data_map' );
        if ( !isset( $userDataMap[$countryAttributeName] ) )
        {
            if ( $requireUserCountry )
            {
                eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
                                       $userObject->attribute( 'name' ) .
                                       "' of class '" .
                                       $userObject->attribute( 'class_name' ) . "'.",
                                     __METHOD__ );
            }

            return false;
        }

        eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
                                       $country, $user->attribute( 'login' ) ) );

        $countryAttribute = $userDataMap[$countryAttributeName];
        $countryAttributeContent = $countryAttribute->content();
        if ( is_array( $countryAttributeContent ) )
            $countryAttributeContent['value'] = $country;
        elseif ( is_object( $countryAttributeContent ) )
            $countryAttributeContent->setAttribute( 'value', $country );
        // not sure that this line is needed since content is returned by reference
        $countryAttribute->setContent( $countryAttributeContent );
        $countryAttribute->store();

        return true;
    }
 function handleUserCountry($orderID)
 {
     // If user country is not required to calculate VAT then do nothing.
     if (!eZVATManager::isDynamicVatChargingEnabled() || !eZVATManager::isUserCountryRequired()) {
         return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
     }
     $user = eZUser::currentUser();
     // Get order's account information and extract user country from it.
     $order = eZOrder::fetch($orderID);
     if (!$order) {
         eZDebug::writeError("No such order: {$orderID}");
         return array('status' => eZModuleOperationInfo::STATUS_CANCELLED);
     }
     if ($user->attribute('is_logged_in')) {
         $userCountry = eZVATManager::getUserCountry($user, false);
     }
     $acctInfo = $order->attribute('account_information');
     if (isset($acctInfo['country'])) {
         $country = $acctInfo['country'];
         // If user is registered and logged in
         // and country is not yet specified for the user
         // then save entered country to the user information.
         if (!isset($userCountry) || !$userCountry) {
             eZVATManager::setUserCountry($user, $country);
         }
     } elseif (isset($userCountry) && $userCountry) {
         // If country is not set in shop account handler, we get it from logged user's information.
         $country = $userCountry;
     } else {
         $header = ezpI18n::tr('kernel/shop', 'Error checking out');
         $msg = ezpI18n::tr('kernel/shop', 'Unable to calculate VAT percentage because your country is unknown. ' . 'You can either fill country manually in your account information (if you are a registered user) ' . 'or contact site administrator.');
         $tpl = eZTemplate::factory();
         $tpl->setVariable("error_header", $header);
         $tpl->setVariable("error_list", array($msg));
         $operationResult = array('status' => eZModuleOperationInfo::STATUS_CANCELLED, 'result' => array('content' => $tpl->fetch("design:shop/cancelconfirmorder.tpl")));
         return $operationResult;
     }
     // Recalculate VAT for order's product collection items
     // according to the specified user country.
     $productCollection = $order->attribute('productcollection');
     if (!$productCollection) {
         eZDebug::writeError("Cannot find product collection for order " . $order->attribute('id'), "eZShopOperationCollection::handleUserCountry");
         return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
     }
     $items = eZProductCollectionItem::fetchList(array('productcollection_id' => $productCollection->attribute('id')));
     $vatIsKnown = true;
     $db = eZDB::instance();
     $db->begin();
     foreach ($items as $item) {
         $productContentObject = $item->attribute('contentobject');
         // Look up price object.
         $priceObj = null;
         $attributes = $productContentObject->contentObjectAttributes();
         foreach ($attributes as $attribute) {
             $dataType = $attribute->dataType();
             if (eZShopFunctions::isProductDatatype($dataType->isA())) {
                 $priceObj = $attribute->content();
                 break;
             }
         }
         if (!is_object($priceObj)) {
             continue;
         }
         // If the product is assigned a fixed VAT type then skip the product.
         $vatType = $priceObj->VATType();
         if (!$vatType->attribute('is_dynamic')) {
             continue;
         }
         // Update item's VAT percentage.
         $vatValue = $priceObj->VATPercent($productContentObject, $country);
         eZDebug::writeNotice("Updating product item collection item ('" . $productContentObject->attribute('name') . "'): " . "setting VAT {$vatValue}% according to order's country '{$country}'.");
         $item->setAttribute("vat_value", $vatValue);
         $item->store();
     }
     $db->commit();
     return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
 }