Hello,

I just realized that there is quite big problem with using of $_SESSION['affiliate'] for user tracking. As you know in ubercart you can set option to create accounts for all anonymous users when they complete checkout. But currently those accounts are not connected with affiliate ID because Drupal create it under different php session when receiving paypal confirmation.

For this case I propose to use affiliate ID from uc_affiliate2_orders table. Just extract it based on order ID.

I talking about this code now:

function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'insert':
      // update user count for affiliate and associate user with affiliate
      $aff = $_SESSION['affiliate'];
      if ($aff) {
        db_query('INSERT INTO {uc_affiliate2_users} (aid, uid) VALUES (%d, %d)', $aff, $account->uid);

        $message = t('User !name referred by user id !affiliate', array('!name' => theme('username', $account),
                                                                        '!affiliate' => theme('placeholder', $aff)
                                                                        )
                     );

        watchdog('user', $message);
      }
      break;

What do you think about that?

Thanks,
Vadim

Comments

vadim.eremeev’s picture

I can suggest to change it into something like:

function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'insert':
// update user count for affiliate and associate user with affiliate
      if (!($aff = $_SESSION['affiliate'])) {
        $aff = db_result(db_query("
          SELECT aid 
          FROM {uc_affiliate2_orders} 
          WHERE order_id = (SELECT order_id FROM {uc_orders} WHERE primary_email = '%s' LIMIT 1) 
            AND aid NOT IN (SELECT aid FROM {uc_affiliate2_users} WHERE uid = %d)
        ", $account->mail, $account->uid));
      }

      if ($aff) {
        db_query('INSERT INTO {uc_affiliate2_users} (aid, uid) VALUES (%d, %d)', $aff, $account->uid);
        $message = t('User !name referred by user id !affiliate', array('!name' => theme('username', $account), '!affiliate' => theme('placeholder', $aff)));
  watchdog('user', $message);
      }
vadim.eremeev’s picture

Even we can optimize query a bit and also need to make primary_email as index in uc_orders table

  $aff = db_result(db_query("
    SELECT uao.aid
    FROM {uc_affiliate2_orders} uao
    INNER JOIN {uc_orders} uo ON uao.order_id = uo.order_id 
    AND uo.primary_email = '%s'
    LEFT OUTER JOIN {uc_affiliate2_users} uau ON uao.aid = uau.aid 
    AND uo.uid = uau.uid 
    WHERE uau.aid IS NULL
  ", $account->mail));