Rakuten LinkShare tracking for Magento success page

A recent job involved creating the relevant code to add a tracking image for a client to the order confirmation page in Magento for Rakuten LinkShare.

Rakuten Linkshare provide a very detailed specification PDF document, which runs to five or so pages – what it boils down to is that the success page has to output an image link, and the source address of that image link contains various bits of information relating to the order that has just been completed.

The end tracking code will need to follow this format :

<img src="http://track.linksynergy.com/ep?mid=xxxx&ord=xx&skulist=xxxx&qlist=xxx&amtlist=xxxx&cur=xxx&namelist=xxxxx">

Where the values are as follows :

“mid” = Merchant ID number (this comes from Rakuten).
“ord” = Order number (the Order Increment ID, in Magento terms)
“skulist” = a list of the SKUs in the order, pipe (|) delimited
“qlist” = a list of the quantities of each SKU in the skulist, also pipe delimited
“amtlist” = the sale price, excluding tax and shipping, of each item in the skulist, also pipe delimited
“cur” = the base currency of the store in ISO three letter country code format
“namelist” = the list of item names for each item in the skulist

Note that the “namelist” entry is optional, but for sake of completeness I’ve included it here.

Since this code snipped is being placed on the template / checkout / success.phtml file (it doesn’t matter where, as the image is invisible) then the OrderID of the relevant order is already available to us. To get started, therefore, our code needs to take that OrderID and load the full order details from it into $order, then load all the visible items (ie. exclude configurable products, just include the underlying simple products) into it :

$lastOrder = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($lastOrder);
$items = $order->getAllVisibleItems();

We’re going to hold the relevant information in four arrays, so let’s instantiate them :

$skus=array();
$quanties=array();
$amounts=array();
$names=array();

Then we need to loop through each item in the array of purchased items and for each one get its SKU, the quantity ordered, the amount paid for each item, and the item name.

foreach($items as $item) {
		$sku=$item->getSku();
		$qty=$item->getQtyOrdered();
		$amount=$item->getPrice();
		$name=$item->getName();

Then add that information to each of the four arrays. Note that the specification requires the amount to be (a) in pence, and (b) the line total, so we’re multiplying it by 100 first, then by the corresponding quantity of items for that line :

		$skus[]=urlencode($sku);
		$quantities[]=urlencode($qty);
		$amounts[]=urlencode(floor($amount*100)*$qty);
		$names[]=urlencode($name);

And finally close off the loop

}

If a discount code has been used, we should pass that information too – Rakuten allows two methods of doing it, but the easiest from Magento’s point of view is adding the discount to the list of products as its own product, like so :

$discountUsed = $order->getCouponCode();
if ($discountUsed != "") {
	$skus[]="Discount";
	$quantities[]="0";
	$amounts[]=($order->getDiscountAmount()) * 100;
	$names[]="Discount";
}

Our four arrays are now bursting at the seams with the relevant information, so it’s time to start building the final output. The Merchant ID will be static, so insert it here, and the Order ID we can get as we did earlier :

$final="<img src=\"http://track.linksynergy.com/ep?mid=XXXXX&ord=".$this->getOrderId();

Then it’s a case of running through each of the four arrays and outputting each item, separated by a pipe. We’re also outputting the currency (hardcoded in this instance, but it wouldn’t be too tricky to make it dependant upon the store settings) :

$final."&skulist=";
$final.=implode("|", $skus);
$final.="&qlist=";
$final.=implode("|", $quantities);
$final.="&amtlist=";
$final.=implode("|", $amounts);
$final.="&cur=GBP&namelist=";
$final.=implode("|", $names);
$final.='">';

So then let’s output the final code :

echo $final;

You can download the entire script snippet at the bottom of this post – hopefully someone will find it comes in handy!