将多个产品列表放入php数据层 [英] List of multiple products into php datalayer

查看:55
本文介绍了将多个产品列表放入php数据层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在订单确认页面"(谢谢您的购买页面)中获得一个很好的数据层跟踪我的网站的营销像素.该网站使用OXID Shop作为电子商务平台.

我想要进行以下设置:

  IF currentPage =='谢谢':products_info ='[{id:'product_id_1',价格:'price_1',数量:'quantity_1'},{id:"product_id_2",价格:"price_2",数量:"quantity_2'}]" 

如果我的页面变量"CurrentController" ='thankyou',我正在尝试以下代码直接从后端获取值,对于仅包含一篇文章值的变量,我可以轻松地做到这一点.

示例:

  [{如果$ currentControllerName =="thankyou"}]var dataLayer = [];[{assign var ="order" value = $ oView-> getOrder()}]dataLayer.push({电子邮件:[{$$ order-> oxorder__oxbillemail-> value}]});[{/如果}] 

问题是,当我需要获取多篇文章的值时,就像我在开始时提到的products_info变量一样.

我正在尝试以下代码,但是我不确定如何使迭代器追加"或将每个产品值发送到一个变量:

  [{如果$ currentControllerName =="thankyou"}]var dataLayer = [];[{assign var ="order" value = $ oView-> getOrder()}]([{foreach from = $ orderArticles item ="currOrderArticle"}][{assign var ="currArticle" value = $ currOrderArticle-> getArticle()}][{assign var ="currBasePrice" value = $ currOrderArticle-> getBasePrice()}]product_info:[{id:[{$ currOrderArticle-> oxorderarticles__oxartnum->值}],价格:[{$ currBasePrice-> getBruttoPrice()}],数量:[{$ currOrderArticle-> oxorderarticles__oxamount->值}]}][{/foreach}])[{/如果}] 

解决方案

您正在调用的Javascript函数.push()每次调用时都会将其追加到dataLayer Array中.(请参见文档)

那么,为什么不聪明地为购物篮中的每个产品呈现对该函数的调用.

  [{如果$ oView-> getClassName()=="thankyou"}]< script>var dataLayer = [];[{assign var ="basket" value = $ oView-> getBasket()}][{foreach from = $ basket-> getContents()item = basketitem}][{assign var = itemarticle value = $ basketitem-> getArticle()}][{assign var ="basketitemprice" value = $ basketitem-> getPrice()}]dataLayer.push({id:[{$ itemarticle-> oxarticles__oxartnum-> value}],价格:[{$ basketitemprice-> getBruttoPrice()}],数量:[{$ basketitem-> getAmount()}]});[{/foreach}]</script>[{/如果}] 

这会将您的产品添加到Javascript dataLayer 数组中.

值得注意的是,以下内容也应起作用:(可能

最后,您要尝试创建一个Javascript数组(可以由Google跟踪代码管理器读取).这是Thankyou页面上的最终输出(也是完全有效的Javascript):

 < script>var dataLayer = [];dataLayer.push({id:3788,价格:24.95,数量:1});dataLayer.push({id:1401,价格:129,数量:1});dataLayer.push({id:10101,价格:0,数量:1});</script> 

请注意,您没有使用PHP将数据附加到dataLayer变量.您正在创建Javascript代码(使用Smarty/PHP),然后将创建dataLayer数组(然后可由Google跟踪代码管理器Javascript使用)

I would like to get a nice data layer tracking for my site's marketing pixels in the "order confirmation page" (thank you for you purchase page). The site uses OXID Shop as the ecommerce platform.

I would like to have the following setup:

IF currentPage == 'thankyou':

products_info = '[{id: 'product_id_1', price: 'price_1', quantity: 'quantity_1'},
                  {id: 'product_id_2', price: 'price_2', quantity: 'quantity_2'}]'

I am trying the following code to obtain the values directly from the backend if my page variable "CurrentController"='thankyou', which I can easily do for a variable that contains only the values of one article.

Example:

[{if $currentControllerName == "thankyou"}]    

    var dataLayer = [];

    [{assign var="order" value=$oView->getOrder()}]

    dataLayer.push(
                   {email: [{$order->oxorder__oxbillemail->value}]}
                   );

[{/if}]

The issue is when I need to get values for more than one article, something like what I mentioned at the beginning for the products_info variable.

I am trying the following code, but I'm not sure how to make the iterator "append" or send each products values to one single variable:

[{if $currentControllerName == "thankyou"}]    

var dataLayer = [];

[{assign var="order" value=$oView->getOrder()}]

(
[{foreach from=$orderArticles item="currOrderArticle"}]
[{assign var="currArticle" value=$currOrderArticle->getArticle()}]
[{assign var="currBasePrice" value=$currOrderArticle->getBasePrice()}]

product_info: [{id: [{$currOrderArticle->oxorderarticles__oxartnum->value}], 
                price: [{$currBasePrice->getBruttoPrice()}], 
                quantity: [{$currOrderArticle->oxorderarticles__oxamount->value}]}]


[{/foreach}]
)

[{/if}]

解决方案

The Javascript function .push() that you are calling appends an item to your dataLayer Array every time you call it. (see docs)

So why not let smarty render the the call to that function for every product that was in the basket.

[{if $oView->getClassName() == "thankyou"}]

<script>

        var dataLayer = [];

        [{assign var="basket" value=$oView->getBasket()}]

        [{foreach from=$basket->getContents() item=basketitem}]

            [{assign var=itemarticle value=$basketitem->getArticle()}]
            [{assign var="basketitemprice" value=$basketitem->getPrice()}]

            dataLayer.push({ id: [{$itemarticle->oxarticles__oxartnum->value}], price: [{$basketitemprice->getBruttoPrice()}], quantity: [{$basketitem->getAmount()}] });

        [{/foreach}]

</script>

[{/if}]

This will add your products to your Javascript dataLayer Array.

It's worthy to note that the following should also work: (might break in unsupported version of IE)

[{if $oView->getClassName() == "thankyou"}]

<script>

    var dataLayer = [

        [{assign var="basket" value=$oView->getBasket()}]

        [{foreach from=$basket->getContents() item=basketitem}]

            [{assign var=itemarticle value=$basketitem->getArticle()}]
            [{assign var="basketitemprice" value=$basketitem->getPrice()}]

            { id: [{$itemarticle->oxarticles__oxartnum->value}], price: [{$basketitemprice->getBruttoPrice()}], quantity: [{$basketitem->getAmount()}] },

        [{/foreach}]

    ];

</script>

[{/if}]

In the end what you are trying to do is creating a Javascript Array (that can be read by Google Tag Manager). This is the final output (and perfectly valid Javascript) on the Thankyou page:

<script>

    var dataLayer = [];

    dataLayer.push({ id: 3788, price: 24.95, quantity: 1 });           
    dataLayer.push({ id: 1401, price: 129, quantity: 1 });        
    dataLayer.push({ id: 10101, price: 0, quantity: 1 });

</script>

Note that you are not using PHP to append the data to the dataLayer variable. You are creating Javascript Code (with Smarty/PHP) that will then create the dataLayer Array (that can then be utilized by Google Tag Managers Javascript)

这篇关于将多个产品列表放入php数据层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆