I need to retrive data from an order, on success page, this info is dynamic and i need to create a script for Dynamic Re-marketing Implementation. This is the script that has to be put after tag in success page, after an order is placed. And i need to retrieve the dynamic info for every order. I managed to include this scripts on cart, on product single page, but on success page it's not working.
this is the script
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': '[product_id]',
'ecomm_pagetype': '[page_type]',
'ecomm_totalvalue': [product_price]
}
});
</script>
In Magento_Checkout/layout/checkout_onepage_success.xml i've added the code to include the phtml file:
<referenceContainer name="head.additional">
<block class="Magento\Framework\View\Element\Template" name="google.adw" before="-" template="Magento_Checkout::googleadw.phtml" />
</referenceContainer>
This is the phtml file that i've added googleadw.phtml
<?php
$orderId = $this->getOrderId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderData = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
$total = $orderData->getGrandTotal();
$skus = '';
foreach($orderData->getAllItems() as $item) {
$skus .= $item->getSku() . ',';
}
$skus = trim($skus, ',');
?>
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': "<?php echo $skus; ?>",
'ecomm_pagetype': "purchase",
'ecomm_totalvalue': "<?php echo $total;?>"
}
});
</script>
The script is placing correctly in but i have an error and can t retrieve data of order.
In cart page, I have access to this info, this is the code for the cart page, and there it's working
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();
// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
// retrieve quote items array
$items = $cart->getQuote()->getAllItems();
$skus = '';
$prices = '';
$quantity = '';
foreach($items as $item) {
// echo 'ID: '.$item->getProductId().'<br />';
$quantity = $item->getQty();
$prices += $item->getPrice() * $quantity;
$skus .= $item->getSku() . ',';
}
$skus = trim($skus, ',');
?>
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': "<?php echo $skus; ?>",
'ecomm_pagetype': "cart",
'ecomm_totalvalue': "<?php echo $prices;?>"
}
});
</script>
there is a difference here, I retrieve the subtotal, not the total, by summing the products with their quantities,but for skus is the same logic.
This is the error i get on succes page
1 exception(s):
Exception #0 (Magento\Framework\Exception\InputException): Id required
Exception #0 (Magento\Framework\Exception\InputException): Id required
#0 /var/www/vhosts/myorganic.ro/staging.myorganic.ro/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Sales\Model\OrderRepository->get(NULL)
#1 /var/www/vhosts/myorganic.ro/staging.myorganic.ro/var/generation/Magento/Sales/Model/OrderRepository/Interceptor.php(26): Magento\Sales\Model\OrderRepository\Interceptor->___callPlugins('get', Array, Array)
So i think that the id it s not retrived, that s why it doesn t work.
Can you please help me?