| Current Path : /var/www/magento.test.indacotrentino.com/www/buy/ |
| Current File : /var/www/magento.test.indacotrentino.com/www/buy/indaco-autumnus-BB.php |
<?php
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get(Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
$redirectFactory = $objectManager->get(\Torresani\QrCodeRedirect\Model\RedirectFactory::class);
$storeId = 2;
$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);
if ($data === null) {
die( "Error in JSON" );
}
$idProduct = (int) $data['productId'] ?? 0;
$action = null;
switch((int) $data['action'] ?? 0) {
case 1: $action = 'detailProduct'; break;
case 2: $action = 'addToCart'; break;
}
if ($action && $idProduct>0) {
insertVisitLog(2, null, $idProduct, $_SERVER['HTTP_REFERER'] ?? '', $action);
}
echo var_dump ($data);
function insertVisitLog($storeId, $customerId, $code, $url, $action) {
try {
$ip = null;
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$operating_system = explode(";",$user_agent)[1]."";
$userAgentParts = explode(" ", $user_agent);
$browser = end($userAgentParts);
$data = [
[
'store_id' => $storeId, 'customer_id' => $customerId, 'code' => $code, 'url' => $url,
'action' => $action, 'ip' => $ip, 'user_agent' =>$_SERVER['HTTP_USER_AGENT'], 'operating_system' =>$operating_system, 'browser' =>$browser,
]
];
$configFile = BP . '/app/etc/env.php';
$config = include $configFile;
$dbConfig = $config['db']['connection']['default'];
$connection = new \mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['dbname']);
if ($connection->connect_error) {
die("Error in connection db: " . $connection->connect_error);
}
$tableName = 'qrcoderedirect_visit';
$insertQuery = "INSERT INTO {$tableName} (store_id, customer_id, code, url, action, ip, user_agent, operating_system, browser) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $connection->prepare($insertQuery);
if ($stmt === false) {
die("Error in query staetment: " . $connection->error);
}
$stmt->bind_param("iisssssss", $storeId, $customerId, $code, $url, $action, $ip, $_SERVER['HTTP_USER_AGENT'], $operating_system, $browser);
$result = $stmt->execute();
if ($result === false) {
die("Error in the query: " . $stmt->error);
}
$stmt->close();
$connection->close();
} catch (\Exception $e) {
// Gestione delle eccezioni
echo "Errore: " . $e->getMessage();
}
}