Affiliate system on Codeigniter

Introduction

I have a friend (yes, you read it right) who is using codeigniter on his e-commerce. He has been looking for a solution to pay comissions to advertisers. I recommended affiliate system for him and came out that he has never even heard about it before. I introduced the concept for him and we started research how it actually works behind the scenes. For me it meant googling and how to actually code it. It felt like a chaos and I did not get any further for a long time. This all affiliate world in google is flooding those afiiliate sites which are advertising everything else except how to design affiliate system.

Finally I found this Stackoverflows thread: http://stackoverflow.com/questions/16231995/how-to-design-an-affiliate-system-on-codeigniter-to-use-on-every-page-on-the-si . That was exact answer to all my questions and after all that mess it was very easy to actually build the very basics of the system. I am going to show how I did it based on that Stackoverflows thread.

How does it work

When advertiser recommends something. He/she will use URL which contains referer id.  If basic URL of the site is something like: http://example.com/products/23423 . With this affiliate system the advertiser would use something like this instead: http://example.com/ref?id=tuukkamerilainen&url=products/23423 . Now the customer who clicks the link will redirect to site and first of all requesting class Ref (ref.php). In ref.php we will make a new cookie which contains information about the advertiser. If the customers will make an order after when “make an order” button is clicked we will check if customer has the cookie containing information about advertiser. If yes then we will save information about order and advertiser to database in same row.

Required changes for the system

  • ref.php to controllers
  • Cookie check and database insert to orders.php or whatever it is in your system(controller which will handle the request when customer makes orders)
  • Table in database for affiliate information

ref.php

[php]

<?php

class Ref extends CI_Controller {

/**
* Index Page for this controller.
* @author      Tuukka Merilainen
* @copyright   10.6.2015
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$url = $this->input->get(‘url’);
$ref = $this->input->get(‘id’);
$cookie = array(
‘name’   => ‘refcookie’,
‘value’  => $ref,
‘expire’ => ‘86500’,
);

$this->input->set_cookie($cookie);
$this->load->helper(‘url’);
redirect($url);
}
}

[/php]

orders.php

[php]

$this->load->helper(‘cookie’);
if (get_cookie(‘refcookie’)) {
$refCookie = $this->input->cookie(‘refcookie’, TRUE);
$order_id = $d[‘order_id’];
$affidata = array(
‘OrderId’ => $order_id,
‘RefId’ => $refCookie
);
$this->db->insert(‘affiliate’, $affidata);
}

[/php]

Database

In this example there is orders table already which has atribute OrderId. We will use it as Foreign Key.

[sql]

CREATE TABLE IF NOT EXISTS `affiliate` (
`afid` int(11) NOT NULL,
`OrderId` smallint(5) unsigned NOT NULL,
`RefId` varchar(255) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

ALTER TABLE `affiliate`
ADD PRIMARY KEY (`afid`), ADD KEY `OrderId` (`OrderId`);

ALTER TABLE `affiliate`
MODIFY `afid` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1;

ALTER TABLE `affiliate`
ADD CONSTRAINT `afiliate_ord` FOREIGN KEY (`OrderId`) REFERENCES `orders` (`OrderId`) ON UPDATE CASCADE;

[/sql]

How to use it

Now when customer comes from advertiser site and makes an order in your site affiliate table will be updated. From that table it is possible to count commissions to advertiser or do what ever you like. It might be good idea to make another view which contains some more information (maybe total amount of the order, etc.).

Conclusion

As you might already noticed this is just the basic template for the affiliate system. It may work in small e-commerce just like this. On long term use and with lots of traffic there is lot to do and happy moments on developing. I hope this guide will give you some ideas where to start and how it would be done.

 

5 Replies to “Affiliate system on Codeigniter”

  1. Sir, I want to know where i have to put the orders.php file code to make it run? Is it a controller, model, or helper or something else?

    1. Hello tushar!

      orders.php is not an actual file. It might be little confusing but meaning of “orders.php” is to point in to a place where your sites (I am assuming that you have an e-commerce site) orders are handled. In my case when customer puts something in a cart and goes further in order process last step is to press “make an order” button. Content of the orders.php should go in that controller where “make an order” button is sending post/get request.

      So it really depends on how the actual site is built. In my case it is in controller called orders.php, but it may be different in your site.

      I hope this helps you, if not feel free to ask more. 🙂

  2. Hello Tushar!

    how to use file ref.php. Help me

    Thanks Tushar

  3. This is a very nice article. Thank you sir!

  4. Absolutely fabulous tutorial ! Just implement it on one of my website. Thank You very much for sharing that easy and powerful solution. In my case it works slightly different – as I used it to track all new users who got registered through referral link. Once new users made transaction, database being updated with whatever amount calculated on the actual price of subscription. Basically, referee can see and “track” purchases of his all referred users.
    It only shows how flexible Your script could be.
    Thanks a lot:)

Leave a Reply