Author Topic: ecommerce datalayer output via orderdone.php  (Read 239 times)

blindfury

  • Beginner
  • *
  • Posts: 1
  • A beginner
ecommerce datalayer output via orderdone.php
« 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:

Code: [Select]
<?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:

Code: [Select]
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!

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9844
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: ecommerce datalayer output via orderdone.php
« Reply #1 on: April 10, 2019, 10:09:08 am »
in general, yes. But you used a lot wrong/old patterns, for example

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

must be
Code: [Select]
$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.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/