An Affiliate Program using Ubercart

We needed to launch a 2-tiered multi-affiliate program for the Ipod Fitness Center (www.ipodfitnesscenter.com) using Drupal. We also use the awesome Ubercart as our Drupal shopping cart, and by using the tools available we were able to build a very nice program to run our new affiliate program.

We first looked at the current Drupal Affiliate module, and felt that since it did not have a Link building function, or a tie-in to Ubercart, we would be better off rolling our own system. We’d hope that this module will continue to be enhanced.

To follow along with this thread, visit www.ipodfitnesscenter.com and login with:
username: demo
password: demo

To become an affiliate, you enter your info in a pretty standard Drupal form; it mainly makes sure we have your mailing address for your check, and your tax id number. Sign up form is here.

http://www.ipodfitnesscenter.com/affiliate

Once you join the program, you can use our site to build image or text links to your website.

http://www.ipodfitnesscenter.com/affiliate/imagelink - To build Image links (cool)
http://www.ipodfitnesscenter.com/affiliate/textlink - to build text links

You'll notice that these both create links that include your affiliate id (which is your Drupal uid) and all other info you need to place these on your website.

The commissions tab is next, and this is where you can see commission that you earn on the site. These commissions are written using Ubercart during the checkout process.

http://www.ipodfitnesscenter.com/affiliate/commission - to see commissions earned

Our site has a 2 tier affiliate program, we pay 20% commission to the affiliate, and 5% to the person that referred the affiliate to the site. Here is the code we used in our affiliate module to write the commissions using Ubercart’s hook_order()

<?php
function affiliate_order($op, $order, $status) // hoook_order for ubercart

{
global
$user;

if (

$op == 'update' && $status == 'pending' && uc_payment_balance($order) <= 1) {
// sale has been made write commision entries
// make sure my afid are set
   
if (empty($_SESSION[afid])) {
       
$_SESSION[afid] = getaffiliateforuser($user->uid, false);
       
$_SESSION[afidup] = getaffiliateforuser($user->uid, true);
    }
   
// get total of commissionalbe products
   
$comm_total = 0;
    foreach (
$order->products as $product) {
       
$x = node_load($product->nid);
        if (
$x->type == "supplements")     $comm_total += $product->price;
    }
    if (
$_SESSION[afid]) {
   
// write primany commission record
       
db_query("insert into {uc_affiliate_pay} (order_id, afid, commission, commission_notes) values ($order->order_id, $_SESSION[afid], $comm_total * .2, '20% commission on $comm_total sale  to $order->billing_first_name $order->billing_last_name')");   
    }
    if (
$_SESSION[afidup]) {
   
// write secondary commission record
           
db_query("insert into {uc_affiliate_pay} (order_id, afid, commission, commission_notes) values ($order->order_id, $_SESSION[afidup], $comm_total * .05, '5% commission on $comm_total sale to $order->billing_first_name $order->billing_last_name')");   

    }
  }
}

?>

The first couple of lines makes sure that my affiliate and affiliate sponsor are already set, (which they should be) they are set also set with hook_user() login, when you click an affiliate link, or in hook_menu(), this is one final check to make sure we have this set.

The next section...

<?php
foreach ($order->products as $product) {
       
$x = node_load($product->nid);
        if (
$x->type == "supplements")     $comm_total += $product->price;
    }
?>

Goes through the products in the order and calculates volume for products we pay commission on. We only pay 20% on the supplements product type. Once I have the total, we write a 20% and a 5% commission to a new table We created called uc_affiliate_pay. It has a simple structure as follows.

CREATE TABLE `uc_affiliate_pay` (
`key` bigint(20) NOT NULL auto_increment,
`order_id` bigint(20) default NULL,
`afid` bigint(20) default NULL,
`commission` double default NULL,
`commission_notes` varchar(60) default NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

The next affiliate tab is 'clicks' which keeps track of every click on an affiliate ad

http://www.ipodfitnesscenter.com/affiliate/clicks

Our affiliate code to track click and route the affiliate links to the correct pages was super simple.

<?php
function affiliate_start(){
$_SESSION[afid] = arg(4);
$target = arg(5);
$_SESSION[afidup] = getaffiliateforuser(arg(4), true);

db_query("insert into (affiliate_clicks} (affiliate_id, click_datetime, target,  referring_site) values ($_SESSION[afid], now(), '$target', '" . $_SERVER['HTTP_REFERER'] ."') ");
switch (
$target) {
case
'home' : drupal_goto("home");
case
'shopping' : drupal_goto("supplements");
case
'go' : drupal_goto("goipod");
case
'join' : drupal_goto("user/register");
case
'flash' : drupal_goto("flash.php");
case
'zeacaps' : drupal_goto("zeacaps");
default :
drupal_goto("supplements");
    }
}
?>

It grabs the affiliate id and the target page from the URL, then finds the sponsoring affiliate, it updates the click database (even tracks the site that sent the click using $_SERVER['HTTP_REFERER'], and then uses drupal_goto to navigate to the target page you selected when you built your link.

Ubercart was a great platform for us to use, and we were able to integrate order reporting easily. By making a call to uc_order_history we let our users see their order detail on a customized user profile page. Check it out here...

http://www.ipodfitnesscenter.com/users/demo

<?php
   
global $user;
    if (
$GLOBALS['user']->uid == arg(1)) {
    echo
'<div style="width: 99%;  padding-left: 1%;">
        <div class="cnt_hd_txt">
        <div id="fadeGreen">
            <div class="cnt_hd">Order History</div>'
;
            echo
uc_order_history($user->uid);
        echo
'</div>
        </div>
    </div>'
;
    }
   
$account = user_load(array('uid' => arg(1)));
   
drupal_set_title("Profile - ". $account->profile_fullname);
   
?>

(Yes Ryan, I did have to use drupal_set_title(), thanks for the tip)

If you click on the order, you'll see that we have including UPS and USPS tracking information in the order comments... so our users can easily see the status of their orders using Ubercart.

http://www.ipodfitnesscenter.com/user/52/order/40

One other thing you may have wondered about when you look at our affiliate link is why we used such a long path in the affiliate link. ie. Here is a sample link...

http://www.ipodfitnesscenter.com/ipod/fitness/nike/affiliate/5/go

We don't actually need the ipod - fitness - nike part of the URL, We could have used only affiliate, but by having our site keywords included in links pointing to our site, it increases our SEO rating for those pages.... (No big deal, but I had someone ask me about that when they made a link a few days ago.)

All in all, we are pretty happy with what this does for our affiliate needs. If you would like source code, or have questions, please feel free to ask... Also, we'd love it if you put up one of my affiliate ads on your website. Again, we pay out 20% on sales, and it's FREE.

Enjoy.
Jim Fulford
www.ipodfitnesscenter.com

PS. We did not build this as a ready to install Drupal module since we did some pretty extensive customization, but if you want to use any of the techniques we use, we’ll be glad to share ideas or source code.