php - Prestashop 增强型电子商务 OrderConfirmation
问题描述
#Analytics #GTM #Prestashop 你好,我带着某件事来到这里。我在网站上嵌入了增强的电子商务代码。但是,我想知道如何防止这段代码变得如此糟糕?OrderConfirmationController.php
我在 Prestashop CMS 文件中插入了这样的代码:
public function displayOrderConfirmation($order)
{
$order = new Order($this->id_order);
$cart = new Cart($order->id_cart);
$productos = $cart->getProducts();
$this->context->smarty->assign(array(
'id_pedido'=> $this->id_order,
'total_a_pagar'=> $order->total_paid_tax_incl,
'gastos_envio'=> $order->total_shipping_tax_excl,
'impuestos'=> ($order->total_paid_tax_incl)-($order->total_paid_tax_excl),
'cupones' => $cupones,
'productos' => $productos
));
return Hook::exec('displayOrderConfirmation', ['order' => $order]);
}
在模板的 TPL 文件中,有一个将数据发送到 GTM 的代码:
{literal}
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'transaction',
'ecommerce': {
'purchase': {
'actionField': {
'id': '{/literal}{$id_pedido}{literal}',
'affiliation': 'Domifito.pl',
'revenue': '{/literal}{$total_a_pagar}{literal}',
'tax':'{/literal}{$impuestos}{literal}',
'shipping': '{/literal}{$gastos_envio}{literal}',
'coupon': '{/literal}{$cupones}{literal}'
},
'products': [{/literal}{foreach from=$productos item=producto name=productos}{literal}
{
'name': '{/literal}{$producto.name}{literal}',
'id': '{/literal}{$producto.id_product}{literal}',
'price': '{/literal}{$producto.price_wt}{literal}',
'brand': '{/literal}{Manufacturer::getnamebyid($producto.id_manufacturer)}{literal}',
'category': '{/literal}{$producto.category}{literal}',
'variant': '',
'quantity': {/literal}{$producto.quantity}{literal},
'coupon': ''
}{/literal}{if $smarty.foreach.productos.iteration != $productos|@count}{literal},{/literal}{/if}{literal}
{/literal}{/foreach}]{literal}
}
}
});
</script>
{/literal}
一切正常,但直到发布一些更新。如何防止该文件被删除。我创建的模块不会向我读取这些数据:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class trochimiak_google_analytics extends Module
{
public $ssl = true;
public $php_self = 'order-confirmation';
public $id_cart;
public $id_module;
public $id_order;
public $reference;
public $secure_key;
public $order_presenter;
public function __construct()
{
$this->name = 'trochimiak_google_analytics';
$this->tab = 'others';
$this->version = '1.0.0';
$this->author = 'Marek Jan Trochimiak';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7',
'max' => _PS_VERSION_
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Enhanced Ecommerce');
$this->description = $this->l('Paczka do google analytics by Marek Jan Trochimiak.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
// określanie w których konkretnych miejscach pojawi się kod
public function install()
{
if (!parent::install() OR
!$this->registerHook('displayOrderConfirmation')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall())
return false;
return true;
}
public function displayOrderConfirmation($order)
{
$order = new Order($this->id_order);
$cart = new Cart($order->id_cart);
$productos = $cart->getProducts();
$this->context->smarty->assign(array(
'id_pedido'=> $this->id_order,
'total_a_pagar'=> $order->total_paid_tax_incl,
'gastos_envio'=> $order->total_shipping_tax_excl,
'impuestos'=> ($order->total_paid_tax_incl)-($order->total_paid_tax_excl),
'cupones' => $cupones,
'productos' => $productos
));
return Hook::exec('displayOrderConfirmation', ['order' => $order]);
}
}
也许你们中的一些人可以帮助我?
解决方案
推荐阅读
- ubuntu - 无法在 ubuntu 21.10 中安装 pgAdmin 4
- flutter - OAuth2 访问授权重定向流程如何在应用内 Web 视图中工作?
- audio - 从终端中的文件向麦克风添加声音
- javascript - 如何使用 css 和 javascript 在 html 中制作温度选择器?
- spring - Spring + JBpm + Bitronix - JTA 事务不回滚
- matlab - MATLAB - 在元胞数组中查找坐标对的索引
- obiee - 基于 OBIEE 中的另一个单元格格式化特定单元格
- javascript - 十进制数的 Joi 验证无法正常工作
- javascript - 如何在chartJs中隐藏两条轴线(选项不起作用)
- r - R为什么我必须为自己分配一个正式的参数变量才能使这个函数工作?