1. Скачать последнюю версию модуля можно по ссылке Модуль GeoIP
2. Для установки модуля, просто распакуйте скачанный архив в корень системы.
3. В основной макет сайты в место где нужно вывести город пользователя вставляем код:
<?php Core_Session::start(); $oMessage = Kad_Message_Fancybox::instance(); if (Core_Array::getPost('shop_country_location_city_id') && Core_Array::getPost('shop_country_location_id') && Core_Array::getPost('shop_country_id')) { $_SESSION['shop_country_location_city_id'] = Core_Array::getPost('shop_country_location_city_id'); $_SESSION['shop_country_location_id'] = Core_Array::getPost('shop_country_location_id'); $_SESSION['shop_country_id'] = Core_Array::getPost('shop_country_id'); } ob_start(); $sIp = Core_Array::get($_SERVER, 'REMOTE_ADDR', Core_Array::get($_SERVER, 'HTTP_X_FORWARDED_FOR', '127.0.0.1')); $oGeoData = Core_Geoip::instance()->getGeoData($sIp); ?> <!--noindex--> <div class="region"> <form id="regions" method="POST" action="."> <?php // Определяем город пользователя (По умолчанию - СПБ) $locationId = 2; $countryId = 175; $cityId = 173; $cityName = 'Санкт-Петербург'; if (isset($oGeoData) ) { // если удалось определить город пользователя $cityId = $oGeoData->cityId; $locationId = $oGeoData->locationId; $countryId = $oGeoData->countryId; if(isset($_SESSION['shop_country_location_city_id'])) { $cityName = Core_Entity::factory('Shop_Country_Location_City', $_SESSION['shop_country_location_city_id'])->name; } else { $cityName = Core_Entity::factory('Shop_Country_Location_City', $cityId)->name; } ?> <div class="is-region" onClick="javascript:;"> <h2>Ваш город <span class="town"><?php echo $cityName; ?>?</span></h2> <br/> <button class="button yes">Да</button> <button class="button no" onclick="$('.is-region').hide(); $.loadLocations('/catalog/cart/', $('#catalog_country_id').val()); $('.region-select').show(); console.log('Событие'); return false;">Другой</button> </div> <?php } else { ?> <div class="is-region"> <h2>Город не определен</h2><br/> <button class="button yes">Закрыть</button> <button class="button no" onclick="$('.is-region').hide(); $.loadLocations('/catalog/cart/', $('#catalog_country_id').val()); $('.region-select').show(); return false;">Выбрать из списка</button> </div> <?php } ?> <div class="region-select" style="display: none"> <label for="shop_country_id">Страна </label> <br/> <select id="catalog_country_id" name="shop_country_id" onchange="$.loadLocations1('/catalog/cart/', $(this).val())"> <?php $aoShopCountry = Core_Entity::factory('shop_country')->findAll(); foreach ($aoShopCountry as $oShopCountry) { $select = ''; if ($oShopCountry->id == $countryId) { $select = "selected='selected'"; } echo "<option value='" . $oShopCountry->id . "' " . $select . ">" . $oShopCountry->name . "</option>"; } ?> </select> <br/> <label for="shop_country_location_id">Область</label><br/> <select name="shop_country_location_id" id="catalog_country_location_id" onchange="$.loadCities1('/catalog/cart/', $(this).val())"> <option value="<?php echo $locationId; ?>" selected="selected">...</option> </select> <br/> <label for="shop_country_location_city_id">Город</label> <br/> <select id="catalog_country_location_city_id" name="shop_country_location_city_id"> <option value="<?php echo $cityId; ?>" selected="selected">...</option> </select> <br/> <button class="button yes">Выбрать</button> </div> </form> </div> <?php $sMessage = ob_get_clean(); $oMessage->init(); $oMessage->add('region', $sMessage); $oMessage->init(); if (!isset($_SESSION['shop_country_location_city_id']) && !isset($_SESSION['shop_country_location_id']) && !isset($_SESSION['shop_country_id'])) { // $oMessage->show(); if (isset($oGeoData)) { $_SESSION['shop_country_location_city_id'] = $oGeoData->cityId; $_SESSION['shop_country_location_id'] = $oGeoData->locationId; $_SESSION['shop_country_id'] = $oGeoData->countryId; } } ?> <span>Ваш город: </span> <a href="" onclick="$('#region').click(); return false;"> <?php if (!is_null($oGeoData)) { echo $_SESSION['shop_country_location_city'] = Core_Entity::factory('Shop_Country_Location_City', $_SESSION['shop_country_location_city_id'])->name; } else { echo 'Определить город не удалось'; } ?> </a>
4. В модуле, если нету папки kad создаем ее. Внутри создаем папку message, в папке размещаем файл fancybox.php и создаем папку fancybox, в которой помещаются файлы из архива fancybox это нужно что бы было всплывающее окно с изменением города
5. Автоопределенный город, чтобы был по умолчанию при оформлении заказа нужно:выполнить преобразование
6. Перед кодом вывода (Пункт 3) вставляем скрипт, чтобы не было потом конфликта с id-(Страны, области и города) в корзине c id выбранным нами города при изменении автоопределения.
<script type="text/javascript"> $.extend({ loadLocations1: function(path, shop_country_id) { $('#catalog_country_location_city_id').clearSelect(); $('#catalog_country_location_city_area_id').clearSelect(); $.clientRequest({path: path + '?ajaxLoad&shop_country_id=' + shop_country_id, 'callBack': $.clientSelectOptionsCallback, context: $('#catalog_country_location_id')}); }, loadCities1: function(path, shop_country_location_id) { $('#catalog_country_location_city_area_id').clearSelect(); $.clientRequest({path: path + '?ajaxLoad&shop_country_location_id=' + shop_country_location_id, 'callBack': $.clientSelectOptionsCallback, context: $('#catalog_country_location_city_id')}); }, loadCityAreas1: function(path, shop_country_location_city_id) { $.clientRequest({path: path + '?ajaxLoad&shop_country_location_city_id=' + shop_country_location_city_id, 'callBack': $.clientSelectOptionsCallback, context: $('#catalog_country_location_city_area_id')}); }, }); $(function(){$('#catalog_country_id').trigger('change');}); </script>
Выполним Пнукт 1 и 2.
3. В основной макет сайты в место где жнужно вывести город пользователя вставляем код:
<?php $sIp = Core_Array::get($_SERVER, 'REMOTE_ADDR', Core_Array::get($_SERVER, 'HTTP_X_FORWARDED_FOR', '127.0.0.1')); echo 'Ваш IP ' . $sIp; $oGeoData = Core_Geoip::instance()->getGeoData($sIp); if (!is_null($oGeoData)) { echo 'Ваш город ' . Core_Entity::factory('Shop_Country_Location_City', $oGeoData->cityId)->name; echo 'Ваш регион ' . Core_Entity::factory('Shop_Country_Location', $oGeoData->locationId)->name; echo 'Ваша страна ' . Core_Entity::factory('Shop_Country', $oGeoData->countryId)->name; } else { echo 'Для вашего IP определить регион не удалось'; } ?>
Страницу создал Вадим Гринев 05.10.16 в 16:14