0
votes

I am trying to add conversion tracking to my magento store. I know I need to add the following code and customize it so magento will share info with CA.

<script type="text/javascript">
var _caq = _caq || [];
var products = [];
products.push({Sku: 'ProductID', UnitPrice: 'item price here', Quantity: 'quantity here'});
products.push({Sku: 'ProductID', UnitPrice: 'item price here', Quantity: 'quantity here'});
_caq.push(["Order", {OrderId: 'OrderID', Revenue: 'oVal', CurrencyCode: '3 letter currency code here', Products: products}]);

so far i have been trying to get the data from the order with the following code:

<?php $orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
$_grand = $order->getGrandTotal();
$custname = $order->getCustomerName();
$itemcount=count($items);
foreach ($items as $itemId => $item)
{
    $sObject2->Item_name__c = $item->getName();
    $sObject2->Unit_price__c = $item->getPrice();
    $sObject2->Sku__c = $item->getSku();
    $sObject2->Quantity__c = $item->getQtyToInvoice();
}

echo $_grand;
echo $custname;
?>

When i try to echo the customers name and grand total, i get a blank for the total and Guest for the customer name. Even if i make the $orderId a number of an order this happens.

2

2 Answers

1
votes

When you're passing in the number of an order are you passing the entity_id or the increment_id? Usually the number you see in admin will be the increment_id. Look in the address bar of the browser to pick out the entity_id.

$order = Mage::getModel('sales/order')->load($orderId); // $orderId should be the entity_id.

Try this method of retrieving the order instead (Works on the success page):

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel("sales/order")->loadByIncrementId($orderId);

Also, make sure to call the correct method for getting the grand total:

$_grand = $order->getGrandTotal(); // Gets the grand total in the store currency.

$_grand = $order->getBaseGrandTotal(); // Gets the grand total in the base currency.

It's probably more useful for your tracking purposes to use the base currency. If you only have one store, or use one currency, it probably won't make any difference. But, if you ever use multiple currencies you'll need to get this right.

-1
votes

i am using these functions please take a look :

function get_all_orders($fromdate,$todate)
{
$getsales = mysql_query("SELECT * FROM `sales_flat_order` WHERE `created_at`>='$fromdate' AND `created_at`<='$todate'");
if(mysql_num_rows($getsales)> 0)
return $getsales;
else
return FALSE;
}

function num_items_under_order($order_entity_id)
{
$getorder_num = mysql_query("SELECT * FROM `sales_flat_order_item` WHERE `order_id`='$order_entity_id'");
if(mysql_num_rows($getorder_num) == 1)
return TRUE;
elseif(mysql_num_rows($getorder_num) > 1 )
return FALSE;
}

function get_order_item_details($order_entity_id)
{
$getsales = mysql_query("SELECT * FROM `sales_flat_order_item` WHERE `order_id`='$order_entity_id'");
if(mysql_num_rows($getsales) == 1)
{
return mysql_fetch_object($getsales);
}
elseif(mysql_num_rows($getsales) > 1)
{
return $getsales;
}
else
return FALSE;
}

Thanks