Files
2024-10-25 14:16:28 +02:00

88 lines
2.1 KiB
PHP

<?php
/**
* Class IMojeSuccessModuleFrontController
*
* @property bool display_column_left
* @property bool display_column_right
*/
class IMojeSuccessModuleFrontController extends ModuleFrontController
{
/**
* Initialize controller
*
* @throws PrestaShopException
* @see FrontController::init()
*/
public function init()
{
$this->display_column_left = false;
$this->display_column_right = false;
parent::init();
}
/**
* @throws PrestaShopException
*/
public function initContent()
{
global $smarty;
parent::initContent();
$gaCartId = (int) Tools::getValue('ga_cart_id');
$gaHash = Tools::getValue('ga_hash');
$gaKey = Configuration::get('IMOJE_GA_KEY');
if(!$gaKey || $gaCartId <= 0) {
$this->setTemplate(IMoje::buildTemplatePath('success', 'front'));
return;
}
$smarty->assign('ga_key', $gaKey);
try {
$cart = new Cart($gaCartId);
if(hash('sha256', $cart->id . $cart->secure_key) === $gaHash) {
$currencyInfo = Currency::getCurrency($cart->id_currency);
$gaConversion = [
[
'command' => 'ecommerce:addTransaction',
'properties' => [
'id' => $cart->id,
'affiliation' => Configuration::get('PS_SHOP_NAME'),
'revenue' => $cart->getOrderTotal(true),
'shipping' => $cart->getTotalShippingCost(),
'tax' => $cart->getOrderTotal(true) - $cart->getOrderTotal(false),
'currency' => $currencyInfo['iso_code'],
],
],
];
foreach($cart->getProducts() as $product) {
$gaConversion[] = [
'command' => 'ecommerce:addItem',
'properties' => [
'id' => $cart->id,
'name' => $product['name'],
'sku' => $product['reference'],
'category' => $product['category'],
'price' => $product['price'],
'quantity' => $product['quantity'],
],
];
}
$smarty->assign('ga_conversion', $gaConversion);
}
} catch(Exception $e) {
Logger::addLog(__METHOD__ . ' ' . $e->getMessage(), 1);
}
$this->setTemplate(IMoje::buildTemplatePath('success', 'front'));
}
}