Ubercart Affiliate System

Posts: 96
Joined: 10/29/2007
Bug Finder

I didn't get much response when I posted this thread, so I've written a pretty cool Affiliate system over the last couple of days. This one cannot be too easily turned into a Drupal module, but I thougt I'd share what I did so others could learn from my experience.

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

To become an affilaite, 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 earnd

My site has a 2 tier affiliate program, I pay 20% commission to the affiliate, and 5% to the person that referred the affilate to the site. Here is the code I use in my affiliate module to write the commissions using ubercarts 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, they are set at login, when you click and affiliate link, or in hook_menu, this is one final check to make sure we have this set.

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

This goes through the products in the order and calcs volume for products I pay commission on. I only pay 20% on the supplements product type. (Note - Ryan, it would be cool if the product type was stored in the $order object somewhere so I would not have to reload all the product nodes.)

Once I have the total, I write a 20% and a 5% commmission to a new table I created called uc_affiliate_pay. (I need to delete coupon discounts from this total still) It has a simple structure with the following structure.

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 for clicks which keeps track of every click on an affilate ad.
http://www.ipodfitnesscenter.com/affiliate/clicks

My 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 then updates the click database (even track the site that sent the click), and then uses drupal_goto to navigate to the target page you selected when you built your link.

All I have left to do is to build a few sexier ads. (I want to do one that rotates, and also one that pulls dynamic content, and one the pulls random products from the ubercart store). I also need to do some additional reporting and summarization so I write the correct commission records over to our payment system.

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

Enjoy.
Jim Fulford
www.ipodfitnesscenter.com

How are people doing Affilate Stuff with Ubercart By: VitaLife (24 replies) Tue, 10/30/2007 - 15:09