VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: blindfury on April 07, 2019, 11:36:44 AM

Title: ecommerce datalayer output via orderdone.php
Post by: blindfury on April 07, 2019, 11:36:44 AM
Hi everyone,

with help of a template script (https://www.phpweb.info/joomla/virtuemart-3-code-track-order-transaction-google-analytic), I have built an ecommerce datalayer output for tracking conversions and values through tag manager.

The shop uses VP One Page Checkout extension from Abhshek Das, however, as I read the orderdone.php doesn't differ from the original thank you page script. I don't want to use the virtueplanet ecommerce extension, I only need the basic values and an conversion event.

Now my updated orderdone.php looks as follows:

<?php
/**
 *---------------------------------------------------------------------------------------
 * @package      VP One Page Checkout - Joomla! System Plugin
 * @subpackage   For VirtueMart 3+
 *---------------------------------------------------------------------------------------
 * @copyright    Copyright (C) 2012-2018 VirtuePlanet Services LLP. All rights reserved.
 * @license      GNU General Public License version 2 or later; see LICENSE.txt
 * @authors      Abhishek Das
 * @email        info@virtueplanet.com
 * @link         https://www.virtueplanet.com
 *---------------------------------------------------------------------------------------
 * $Revision: 27 $
 * $LastChangedDate: 2018-10-08 11:59:01 +0530 (Mon, 08 Oct 2018) $
 * $Id: orderdone.php 27 2018-10-08 06:29:01Z Abhshek Das $
 * --------------------------------------------------------------------------------------
*/
defined('_JEXEC') or die;

echo 
'<div class="vm-wrap vm-order-done">';

if(
$this->display_title)
{
    echo 
'<h3>' vmText::_('COM_VIRTUEMART_CART_ORDERDONE_THANK_YOU') . '</h3>';
}

// Everything here is displayed by payment method plugin.
// It is exactly same as standard VirtueMart order done layout. We just need to print it as it is.
echo $this->html;

if(
$this->display_loginform && !JFactory::getUser()->guest && class_exists('shopFunctionsF'))
{
    echo 
shopFunctionsF::getLoginForm();
}

echo 
'</div>';

/***** Virtuemart dataLayer for tracking purposes starts here ********/

// GET ORDER DETAILS
require_once(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'orders.php');
$order VirtueMartModelOrders::getOrder($this->cart->virtuemart_order_id);
$order_id $order['details']['BT']->order_number;
 
// START DATALAYER WITH TRANSACTION DETAILS
$q "SELECT order_status, order_number, order_total, order_tax, order_shipping FROM #__{vm}_orders WHERE ";
$q .= "#__{vm}_orders.user_id= " $auth["user_id"] . " ";
$q .= "AND #__{vm}_orders.order_id= $order_id ";
 
$db->query($q);
if (
$db->next_record()) {
    
$order_status $db->f("order_status");
    
$order_number $db->f("order_number");
    
$order_total $db->f("order_total");
    
$order_tax $db->f("order_tax");
    
$order_shipping $db->f("order_shipping");
 
    if(
$order_status == PAYPAL_VERIFIED_STATUS || $order_status == PAYPAL_PENDING_STATUS) {
 
    
$myTagString "
        <script>
             window.dataLayer = window.dataLayer || [];
             dataLayer.push({
                'event': 'purchase',
                'transactionId': '
$order_number',
                'transactionTotal': 
$order_total,
                'transactionProducts': ["
;
 
// CHECK FOR ORDERED ITEMS AND ADD TO DATALAYER
$q2 "SELECT * FROM #__{vm}_order_item WHERE #__{vm}_order_item.order_id= $order_id";
$db->query($q2);
while (
$db->next_record()) {
    
$item_sku $db->f("order_item_sku");
    
$item_name $db->f("order_item_name");
    
$item_price $db->f("product_final_price");
    
$item_quantity $db->f("product_quantity");
    
    
$myTagString .= "
    {
        'sku': '
$order_item_sku',
        'name': '
$order_item_name',
        'price': 
$product_final_price,
        'quantity': 
$product_quantity
    },"
;
 
 }

$myTagString substr($myTagString0, -1);
$myTagString .= "
]
 });
</script>
 "
;

echo 
$myTagString;
 
/***** Virtuemart dataLayer for tracking purposes ends here ********/



Should this solution work? There's one line from the original script that caught my attention:

if($order_status == PAYPAL_VERIFIED_STATUS || $order_status == PAYPAL_PENDING_STATUS)

With additional payment options, there should be more than the paypal status in the statement, right?

Neither a php/js nor vm crack here, so I'd be thankful for your help.

Thanks!
Title: Re: ecommerce datalayer output via orderdone.php
Post by: Milbo on April 10, 2019, 10:09:08 AM
in general, yes. But you used a lot wrong/old patterns, for example


require_once(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'orders.php');
$order = VirtueMartModelOrders::getOrder($this->cart->virtuemart_order_id);


must be

$model = VmModel::getModel('orders');
$order = $model->getOrder($this->cart->virtuemart_order_id);


also the
$q2 = "SELECT * FROM #__{vm}_order_item WHERE #__{vm}_order_item.order_id= $order_id";

is old vm1 style. Use the vmdebug function and enable the vmdebug in vm config to see which data is already there. For example the order is imho already loaded with all items.
Title: Re: ecommerce datalayer output via orderdone.php
Post by: theo93 on April 16, 2020, 15:12:15 PM
Quote from: Milbo on April 10, 2019, 10:09:08 AM
in general, yes. But you used a lot wrong/old patterns, for example


require_once(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'orders.php');
$order = VirtueMartModelOrders::getOrder($this->cart->virtuemart_order_id);


must be

$model = VmModel::getModel('orders');
$order = $model->getOrder($this->cart->virtuemart_order_id);


also the
$q2 = "SELECT * FROM #__{vm}_order_item WHERE #__{vm}_order_item.order_id= $order_id";

is old vm1 style. Use the vmdebug function and enable the vmdebug in vm config to see which data is already there. For example the order is imho already loaded with all items.

Hello,

If i want the orders should i change this code and paste it in here: /html/com_virtuemart/cart/order_done.php

gtag('event', 'purchase', {
"transaction_id": "24.031608523954162",
"affiliation": "Google online store",
"value": 23.07,
"currency": "USD",
"tax": 1.24,
"shipping": 0,
"items": [
{
"id": "P12345",
"name": "Android Warhol T-Shirt",
"list_name": "Search Results",
"brand": "Google",
"category": "Apparel/T-Shirts",
"variant": "Black",
"list_position": 1,
"quantity": 2,
"price": '2.0'
},
{
"id": "P67890",
"name": "Flame challenge TShirt",
"list_name": "Search Results",
"brand": "MyBrand",
"category": "Apparel/T-Shirts",
"variant": "Red",
"list_position": 2,
"quantity": 1,
"price": '3.0'
}
]
});

What parameters should i use?
Title: Re: ecommerce datalayer output via orderdone.php
Post by: pinochico on April 16, 2020, 16:49:23 PM
I don't use order_done.php

I use GTM a and GTM Virtuemart ecommerce plugin and all value are setup on GTM :D

Sometimes this is not finally page and than your order is will not be send to GA :)