0

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.

Adam Capps
  • 119
  • 3
  • 14

2 Answers2

1

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.

Luke Mills
  • 1,616
  • 1
  • 11
  • 18
-1

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

Sandeep Singh
  • 161
  • 2
  • 16
  • That doesn't really help. I need this code to be in the success page. I need it to track conversions for some advertising campaigns. – Adam Capps Jan 07 '14 at 20:10
  • This is sooo wrong on so many levels. You should not be using SQL queries directly in Magento. You can achieve the same thing by using the respective collection objects and filters. You should not be using mysql_ functions (See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) - they open your code to the possibility of sql injection. – Luke Mills Jan 07 '14 at 22:22
  • yes this is wrong way.. but if you want to do something fast and easier then this is the only way if you are handling 1000's of orders... with mage.php very slow – Sandeep Singh Jan 08 '14 at 20:45