问题
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}]
回答1:
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)
来源:https://stackoverflow.com/questions/36494128/list-of-multiple-products-into-php-datalayer