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!

11 comments

  • caleb

    How can I remove the ‘&amp’ and just have ‘&’ in the code:

    p.s. GREAT ARTICLE !!! THANKS!!!

    • Giles (author)

      “&” and “&” are one and the same when it comes to HTML.

      Kind regards,

      Giles

  • Rich Westbrook

    This is a great tutorial! I am only having one problem. The guys at linksshare say that “The &qlist parameter is a decimal figure, we are looking for a whole number (in this case 1 versus 1.0000)”

    do you know how I can fix this?

    • Giles (author)

      Rich,

      You should be able to do that by changing :

      		$qty=$item->getQtyOrdered();
      

      to

      		$qty=round($item->getQtyOrdered(),0);
      

      That will round 1.0000 to 0 decimal places, ie. 1.

      Giles

  • Etienne

    Thanks, very helpful!

  • Vikram

    Thanks a ton Giles…keep up the good work..:)

    • Giles (author)

      You’re very welcome.

  • Vikram

    Thanks for real help..But one question here.

    What would be the currency ? Will it be Base Currency or Currency selected by customer from currency switcher in Magento ?

    Thanks.

    • Vikram

      Also how can I add LS pixel only for specific Payment methods like Paypal and CC orders ?

      • Giles (author)

        You would want to get the payment method used for the order, and then wrap the entire code in an if block – so if the payment method is X or Y, carry on and execute the remainder of the script – if not, don’t.

        Giles

    • Giles (author)

      Vikram,

      Thanks for your comment – it’ll be the base currency of the transaction.

      Kind reagrds,

      Giles

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk to us about your project?