give referrers a unique code to add to the end of their links to you. Add a small script into the on session start function to see if the code exists. If it does then add a session variable or cookie (depending on how many hours/ days the user has to buy within for the refer to get their commission) and then on the cart system simply transfer the reference id into the orders table. If you want more than that you can do page logging etc