Answer the question
In order to leave comments, you need to log in
How to implement the definition of user cities?
Good day.
From time to time I make websites on Bitrix.
But I never made a definition of a store where you need to determine the user's city and show him the prices
for his city and the goods that he also has in the city. I have created cities, there is a divided product by city and there are different prices. Now the question is how to determine the coordinates (geolocation is not the main problem) and show (!)
Products from the desired city. If there are tips or specific examples, or a way to implement it, or where to read to figure it out, I will be grateful.
Answer the question
In order to leave comments, you need to log in
You need a module an IP location module and a product CCatalogProductProvider
Just 3 lines of code:
require $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'; // connect the Bitrix prologue
require_once($_SERVER["DOCUMENT_ROOT"] . "/ADRES_DO_CLASSA_OTNOSITELNO_KORNYA_SAITA/nikolaevevgeIpGeo.php"); // include the library class
var_dump(nikolaevevgeIpGeo::getInfoByIp($_SERVER["REMOTE_ADDR"])); // get the var_dump of the array as a result of the library's work to determine the client's geography by IP address
The library class itself is available at the link blog.ivru.net/?id=82
Installing the library :
- the second listing - SQL code for creating a table in the Bitrix database, where the cache will be stored for the IP addresses on which the request was made to the remote server, so as not to perform repeated remote connections, as well as to reduce the detection time;
- the third listing is the library class itself, its code must be saved, for example, to the nikolaevevgeIpGeo.php file and included in the first line of the code that I gave in my answer.
Description of the library class :
The library is quite simple. Contains methods for converting IP addresses to an integer without dots and vice versa, as well as methods for searching the cache in the local database, saving new cache data, and a method for requesting information from the server where the database is stored by addresses.
Geography information defined by one IP address within a single PHP session does not create repeated calls to the database cache and the remote server, by storing information about the IP address in a static variable of the library class, that is, if information about the IP is in the specified array, queries to a remote server or database will not be executed. This approach works even if you use the definition from different Bitrix modules.
Library settings (optional):
The library class has these lines:
// [START] !! Settings !!
static public $timeToUpdate = 1728000;// Time in seconds after which records about IP addresses in the database are considered outdated (1728000 = 20 days).
static public $tableName = "useripdata";// Table name in the database
// [END] !! Settings !!
In the first variable, you can set the storage time for the cache with information about the IP address, by default, 20 days.
The second variable specifies the name of the table in the database that you want to use to store the cache with geo-information about the IP address (or rather about the stack of IP addresses), if you use something other than useripdata, then do not forget to change the name of the table when creation in the database, namely in the line CREATE TABLE IF NOT EXISTS `useripdata` (
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question