I'm trying to get Avantlink's code into my order confirmation page. The code should look like this when it's done:

<script type="text/javascript">
var _AvantMetrics = _AvantMetrics || [];
_AvantMetrics.push(['order',{ order_id:'[ORDER_ID]', amount:'[ORDER_AMOUNT]', state:'[BILLING_STATE]', country:'[BILLING_COUNTRY]' }]);
_AvantMetrics.push(['item',{ order_id:'[ORDER_ID]', parent_sku:'[ITEM_PARENT_SKU]', variant_sku:'[ITEM_VARIANT_SKU]', price:'[ITEM_PRICE]', qty:'[ITEM_QUANTITY]' }]);

Using this stack exchange article as a reference for adding success page confirmation: Add tracking code to success.phtml

Trying to put this into the bottom of success.phtml

<script type="text/javascript">
var _AvantMetrics = _AvantMetrics || [];
$newOrderId = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId()); 
$newOrder = Mage::getModel('sales/order')->load($newOrderId);
$items = $newOrder->getAllItems();
$currency = Mage::app()->getStore()->getCurrentCurrencyCode();
$billingAddress = $newOrder->getBillingAddress();

echo "_AvantMetrics.push(['order',{ order_id:'{$newOrder->getId()}', amount:'{$newOrder->getGrandTotal()}', state:'{$billingAddress->getRegionId();}', country:'{$billingAddress->getCountry();}' }]);";

foreach ($items as $item){
    echo "_AvantMetrics.push(['item',{ order_id:'{$newOrder->getId()}', parent_sku:'{$item->getSku()}', variant_sku:'{$item->getSku()}', price:'{$item->getPrice()}', qty:'{$item->getQtyOrdered()}' }]);";
} ?>

But it doesn't work. When I do this, the success page breaks. Where am I going wrong?

closed as off-topic by liyakat, Marius Jul 31 '15 at 15:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced OR seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Marius
If this question can be reworded to fit the rules in the help center, please edit the question.


Figured this out. Here's my latest version of the code.

$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$amount = $order->getSubtotal();
$orderid = $order->getIncrementId();
$items = $order->getAllItems();

echo "<script type=\"text/javascript\">";
echo "var _AvantMetrics = _AvantMetrics || [];
_AvantMetrics.push(['order',{ order_id:'{$orderid}', amount:'{$amount}', state:'', country:'' }]);";

foreach($items as $item) {
  $_sku = $item->getSku();
  $_itemprice = $item->getPrice();
  $_itemqty = $item->getQtyOrdered();
  echo "_AvantMetrics.push(['item',{ order_id:'{$orderid}', parent_sku:'{$_sku}', variant_sku:'{$_sku}', price:'{$_itemprice}', qty:'{$_itemqty}' }]); ";
echo "</script>"

Echoed the entire javascript. And moved the magento calls to variables. Magento calls define variables in the foreach loop. And then the variables called in the echo.

The only issue with this code is that I don't differentiate between the parent & variant skus. This is fine for my purposes, but if you've got a complex site with lots of configurable products or custom options, you're going to want to figure that bit of code out.

Edit: I created a gist for this too: https://gist.github.com/sam327/d519b0ad78500dfc8ec1

Not the answer you're looking for? Browse other questions tagged or ask your own question.