Commission Junction – How To Get It To Work On Magento

Share On Facebook
Share On Twitter
Share On Linkedin

commission-junction-banner

Affiliate Marketing typically involves a company promoting another company’s product and earning a commission.  It is a type of performance-based marketing using a network of businesses that is rewarded based on the promotion of another business’s product.

The benefits of affiliate marketing include having relationships between three parties: the advertiser, the publisher, and the consumer.

We recently had the opportunity to work with Commission Junction, an affiliate network that began in 1998.  Our client wanted Commission Junction integrated into their Magento Enterprise installation.

Commission Junction allows you to post the product information, as well as information about the order, from your Magento checkout success page back to the Commission Junction server. Their API integration offers a few ways to perform this task, however, the most efficient way to get it working is to use their ‘Iframe Solution’. An Iframe is basically an html tag that allows a website to include the contents of another website. In this scenario, Commission Junction provides you with a basic Iframe html tag, which includes a reference to one of their servers which is used to process the order information after each sale, and it is your job to dynamically populate that Iframe with the order parameters such as the items, their price, their quantity and any discounts or coupons that were applied to the sale. Below is an example of what the output would look like:

[iframe src="https://www.emjcd.com/tags/c?containerTagId=44444&CID=15442222&OID=100000789&TYPE=376897&ITEM1=1111222222&AMT1=109.85&QTY1=3&CURRENCY=USD" name="Conversion tag" width="1" height="1" frameborder="0" scrolling="no"]

You will notice the following parameters:

CID
ContainerTagId
OID (Order ID)
TYPE
ITEM1
AMT1
QTY1
CURRENCY

CID, TYPE and ContainerTagId are values that commission junction will assign to you, as they are account specific. The other fields are order ID, Item 1, Amount 1 (the sale price) and quantity 1. If you have 3 products, you would have to pass the sku, quantity and price of all 3 items. Each item, such as the first, are listed as ITEM1, QTY1, AMT1 whereas the second product would list the parameters as ITEM2, QTY2, AMT2 etc. The same logic applies for all products. This pattern allows commission junction to process all items of your order. Below is how I was able to dynamically construct this string for every order that takes place for our client. We would load this Iframe code on the success page of the checkout as, at that point, we would instantly receive the parameters we need to pass to Commission Junction. In order to load the data to pass to Commission Junction, we use two functions, both of which are provided by the tech support team at Commission Junction. These functions are described below. Please note, I have found that there are some modifications that need to be made to them. Below is the code I used for each function, along with an explanation of its purpose, in order to get this solution to work.

# Functions #

1.) _getOrderProductList()

This function obtains the quantity, sku and price of each product that was ordered. While you will be provided with this function from the tech support at Commission Junction, I have found this modification helps – see below:


private function _getOrderProductsList($order) {
$orderItems = $order->getAllItems();
$purchasedSkus = array();
$count_orderItems = count($orderItems);

for($i = 0; $i < $count_orderItems; $i++) { $item=$orderItems[$i]; $product_type=$item->getProductType();

$price = $item->getPrice();

if($price>”0.00″)
{

$purchasedSkus[$i] = array(
‘ITEM’ => $orderItems[$i][‘sku’],
‘QTY’ => number_format($orderItems[$i][‘qty_ordered’],0), // no decimals
‘AMT’ => number_format($orderItems[$i][‘price’],2) // 2 decimal places

// Per C.J, the following line should be commented out.

// ‘DCNT’ => number_format(abs($orderItems[$i][‘discount_amount’]),2)
);

} // end if if price > 0.00

} // end for statement

2.) getCommissionJunctionUdo()

If you need help integrating Commission Junction with your Magento store, call 845-656-3000 or Contact us here »

This function has a parameter, $orderId, which is passed from the success page. Once it is passed it populates two values that Commission Junction can provide you with:

CID Number
Account Type Number

public function getCommissionJunctionUdo($orderId) {
$order = Mage::getModel('sales/order')->load($orderId);
$udo = array();
$udo['CID'] = 'your_account_cid_number';
$udo['TYPE'] = 'your_account_type_number';
$udo['CURRENCY'] = 'USD';
# $udo['OID'] = $orderId;

# Load increment ID

$udo[‘OID’] = $order->getIncrementId();
$udo[‘DISCOUNT’] = number_format(abs($order->discount_amount),2);

$order_coupon_code = $order->coupon_code;
if(!is_null($order_coupon_code) && !empty($order_coupon_code)) {
$udo[‘COUPON’] = $order_coupon_code;
}

$udo[‘PRODUCTLIST’] = self::_getOrderProductsList($order);

$masterTmsUdo[‘CJ’] = $udo;
$json_masterTmsUdo = json_encode($masterTmsUdo);
$json_masterTmsUdo=trim($json_masterTmsUdo, “\x0”);

return $json_masterTmsUdo;

}

We leave this code alone as it performs the task of encoding the data from the order into Json, which we then decode into the parameters that you saw in the Iframe demo that I posted. In order to bring these functions together, I had to perform the following tasks:

1.) Create A Module

We created a module called Shero_Commission

app/etc/modules/Shero_Commission.xml

app/code/local/Shero/Commission

–> etc/config.xml

–> Helper/Data.php

Data.php used this class:

class Shero_Commission_Helper_Data extends Mage_Core_Helper_Data
{

And that class contained the two functions I showed you. Nothing else. At this point, we have a basic module with a helper file that I reference from the success page of the checkout. That template is located in our child theme under /template/checkout/success.phtml

In success.phtml, we use the following code to reference the Helper file, which parses all of our order information, and we then pass it into a dynamic string which includes all relevant order information we then append to the Iframe. Below is the code I use in the success.phtml file:

function clean($string) {

$string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens.

return preg_replace(‘/[^A-Za-z0-9\-]/’, ”, $string); // Removes special chars.
}

$oid = $this->escapeHtml($this->getOrderId());

# Let’s get order object and load it by this increment ID that is provided to us

$order=Mage::getModel(“sales/order”)->loadByIncrementId($oid);

# Let’s get order ID

$order_entity_id = $order->getId();

# WE now call the module’s helper file and the method that creates the JSON string from the order that was just processed.

$cj_helper = Mage::helper(‘commission’)->getCommissionJunctionUdo($order_entity_id);

# We now decode the JSON output.
$new_helper=json_decode($cj_helper);

# We establish a variable, cj_string, which will contain the entire dynamic string of all order parameters and account for the item1, item2, item3 logic
# It is imperative that each unique item has the proper number associated with it, so the first item is item1 and the second is item2, etc.

$cj_string=””;

# Grab discount and coupon info from the order, if they exist

$discount = number_format(abs($order->discount_amount),2);
$order_coupon_code = $order->coupon_code;

# We are now going to loop through all of the products listed from the helper. The 1 value for $x is part of the loop, as each item needs a digit related to the # of item
# we are appending to the string. So first item has item1 label, item #2 has item2 label/etc. We append $x to the item, qty and amt labels for each product we loop through
# as this matches the pattern Commission Junction requires from us.

$x=”1″;
foreach ($new_helper->CJ->PRODUCTLIST as $productlist) {

# Get each item sku
$item=$productlist->ITEM;

# Clean up the string, as some skus contained special characters that interfered with Commission Junction’s processing of the order

$item=clean($item);

# Get the amount
$amount=$productlist->AMT;

# Get the Quantity
$qty=$productlist->QTY;

# Format the string so all item,qty and amount parameters are passed into the Iframe for each item we loop through.

$cj_string.=”ITEM”.$x.”=$item&AMT”.$x.”=$amount&QTY”.$x.”=$qty&”;

$x++;
}

# Let’s add to the string ‘cj_string’ which will be added to the iframe block of code.

$cj_string.=”DISCOUNT=$discount&COUPON=$order_coupon_code&”;

The final line of code is the Iframe block itself:

You will see, in bold, are the variables oid (order id) and cj_string, which is what we generated in the code that I have referenced from this success.phtml file. All you have to do is make sure to have values populated for the containerTagId, CID and ‘type’, all of which can be provided from Commission Junction. Once your module is set up, you can run a test with tech support at Commission Junction and place test orders and make sure the data was passed along to their servers. If so, you are set going forward. I recommend you take the time to test it a few times and really make sure everything runs perfectly. The best idea is to test a few variations of an order:

Order With Discount
Order Without Discount
Order With And Without Multiple Quantities And Multiple Items

Cover every base and make sure the format is perfect for each possible order scenario. When you do this, you will have an automated solution for processing orders with Commission Junction.

Rate this post