HummingbirdUK main logo

HummingbirdUK

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Rakuten LinkShare tracking for Magento success page

Home / Blog / Rakuten LinkShare tracking for Magento success page

Written by Giles Bennett

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 : [php] <img src="http://track.linksynergy.com/ep?mid=xxxx&ord=xx&skulist=xxxx&qlist=xxx&amtlist=xxxx&cur=xxx&namelist=xxxxx"> [/php] 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 : [php] $lastOrder = $this->getOrderId(); $order = Mage::getModel('sales/order')->loadByIncrementId($lastOrder); $items = $order->getAllVisibleItems(); [/php] We're going to hold the relevant information in four arrays, so let's instantiate them : [php] $skus=array(); $quanties=array(); $amounts=array(); $names=array(); [/php] 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. [php] foreach($items as $item) { $sku=$item->getSku(); $qty=$item->getQtyOrdered(); $amount=$item->getPrice(); $name=$item->getName(); [/php] 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 : [php] $skus[]=urlencode($sku); $quantities[]=urlencode($qty); $amounts[]=urlencode(floor($amount*100)*$qty); $names[]=urlencode($name); [/php] And finally close off the loop [php] } [/php] 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 : [php] $discountUsed = $order->getCouponCode(); if ($discountUsed != "") { $skus[]="Discount"; $quantities[]="0"; $amounts[]=($order->getDiscountAmount()) * 100; $names[]="Discount"; } [/php] 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 : [php] $final="<img src=\"http://track.linksynergy.com/ep?mid=XXXXX&ord=".$this->getOrderId(); [/php] 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) : [php] $final."&skulist="; $final.=implode("|", $skus); $final.="&qlist="; $final.=implode("|", $quantities); $final.="&amtlist="; $final.=implode("|", $amounts); $final.="&cur=GBP&namelist="; $final.=implode("|", $names); $final.='">'; [/php] So then let's output the final code : [php] echo $final; [/php]
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.