首页 > 解决方案 > 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]);
    }



}

也许你们中的一些人可以帮助我?

标签: phpgoogle-analyticsprestashopgoogle-tag-managerenhanced-ecommerce

解决方案


推荐阅读