WordPress Affiliate Manager

Affiliates Management Plugin for WordPress

  • Home
  • Features
    • Screen Shots
  • Download
  • Documentation
  • Addons
  • Support
    • Support Forum
    • Forum Login
    • Forum Registration
    • Contact
    • Premium Addon/Plugin Support

Get Affiliate's Username by their Affiliate ID/RefKey

by

WordPress Affiliate Manager › Forums › Affiliate Manager Plugin › Get Affiliate's Username by their Affiliate ID/RefKey

Tagged: affiliate id, get, invite code, paid membership pro, refkey, username

  • This topic has 16 replies, 2 voices, and was last updated 11 years, 5 months ago by admin.
Viewing 15 posts - 1 through 15 (of 17 total)
1 2 →
  • Author
    Posts
  • October 4, 2014 at 9:38 am #284
    christianf3
    Participant

    Hi,

    I’m in the process of adding an invite code system (using PM Pro’s discount codes) to the PM Pro Integration plugin and I need a way to get the username of an affiliate using that affiliate’s id or RefKey. Is there a quick function, mysql query, or some other code that I can use to do this?

    My goal with this is to convert the RefKey into a discount code, but I want to use the affiliate’s username as the code itself.

    October 7, 2014 at 6:39 am #291
    admin
    Keymaster

    We have now made it so you can use the Affiliate’s ID as the refkey value. So all you have to do is use the Affiliate’s ID or their email address as the code itself.

    Lets say the affiliate’s ID is 2 (you can see this value in the admin dashboard). So, now you can make the following referral URL that will give commission to this affiliate:

    
    http://localhost:81/wp/wp7?wpam_refkey=2
    

    or lets say the affiliate’s email address is [email protected] then you can use the following as the referral URL also:

    
    http://localhost:81/wp/[email protected]
    
    October 8, 2014 at 9:40 am #303
    christianf3
    Participant

    Excellent! Nice work.

    I must say, the affiliate’s username is still the best case scenario, because it’s not too short like the Affiliate ID, and it’s not too long like the encrypted refkey string. My goal is to use the refkey as an invite code that is required to register for my site, and ultimately I would like to be able to use these codes in printed media as well.

    Any suggestions on how I can generate a username-based invite code off of the affiliate’s ID or encrypted refkey?

    Many thanks for your help with this.

    October 9, 2014 at 6:25 pm #309
    christianf3
    Participant

    I managed to find a hidden PM Pro Invite Only Addon which has saved me many hours of development time. So now, I’m in the process of modifying the WP Affiliate Manager – PM Pro Integration plugin to do 2 things:

    1) Automatically add the referring affiliate’s Invite Code during registration.

    2) If no refkey is set, automatically set the refkey based on the invite code of the referring affiliate.

    I have all of the code written out to execute this logic already, and I’m in the debug process. The only problem I’m having is with the SQL Queries that pull the affiliate’s data.

    This is what my queries look like:

    		//get affiliate's user id if there is an affiliate refkey - NEED TO FIX THIS QUERY
    		$aff_user_id = $db->get_var( $db->prepare( "SELECT userId FROM {$db->$affiliates_table} WHERE meta_key = 'uniqueRefKey' AND meta_value='%s'", $affiliate_code ) );
    		//get affiliate's user id by the invite code	
    		$aff_user_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'pmpro_invite_code' AND meta_value='%s'", $affiliate_code ) );
    		//get affiliate's refkey by affiliate's user id
    		$aff_RefKey = $wpdb->get_var( $wpdb->prepare( "SELECT uniqueRefKey FROM {$wpdb->wpam_affiliates} WHERE meta_key = 'userId' AND meta_value='%s'", $aff_user_id ) );

    Is there a special way that I need to access the data from the affiliate repository so that I can get the affiliate’s user id by their refkey and get their refkey by their user id?

    I’m excited about getting this done to contribute to making this plugin even better. Thanks again.

    October 9, 2014 at 10:25 pm #312
    admin
    Keymaster

    You can get the refkey using their ID or email address but not the other way around.

    October 10, 2014 at 10:14 am #314
    christianf3
    Participant

    When you say that you can get the refkey using their ID, do you mean their wordpress user id, or their affiliate id? And what’s the best way to get the affiliate’s refkey by their ID?

    October 11, 2014 at 12:07 am #316
    admin
    Keymaster

    I mean their Affiliate ID. The value of the “AID” column from the affiliate plugin.

    Here is an example snippet of code

    
    $aid_value = "2";//TODO set the value of the affiliate ID
    $db1 = new WPAM_Data_DataAccess();
    $affiliateRepos1 = $db1->getAffiliateRepository();
    $affiliate = $affiliateRepos1->loadBy(array('affiliateId' => $aid_value, 'status' => 'active'));
    if ( $affiliate === NULL ) {  //affiliate with this ID does not exist
    
    }
    else
    {
        $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
        if(!empty($default_creative_id))
        {
            $creative = $db1->getCreativesRepository()->load($default_creative_id);
            $linkBuilder = new WPAM_Tracking_TrackingLinkBuilder($affiliate, $creative);
            $strRefKey = $linkBuilder->getTrackingKey()->pack();
            
            //TODO - do something with the $strRefKey value
        }
    }
    
    October 11, 2014 at 1:23 pm #319
    christianf3
    Participant

    Thanks, that explains how to get the refkey. Now, my only challenge is getting the affiliate’s id.

    What about the getAffiliateIdFromRefKey() function? Is there a way to use this function to get the affiliate’s id by passing the $strRefKey (which could contain either $_COOKIE[WPAM_PluginConfig::$RefKey], or $request[WPAM_PluginConfig::$RefKey]) variable into it?

    Once I have the affiliate’s id, I just need a way to get their wordpress user id from the Affiliate Repository, so that I can use it to get their invite code via get_user_meta(). Is it possible to query the database for the affiliate’s user id using the affiliate id? I see that the data is stored in the wpam_affiliates table, but I haven’t been able to successfully pull it.

    October 16, 2014 at 5:18 am #323
    christianf3
    Participant

    Hi, any thoughts on this?
    So far WP Affiliate Manager is exactly what I’m looking for, but my system requires that I have this invite code system connected to the affiliate system, and I at least need to know how to query the database for the affiliate’s info to get this to work for me. I am on a timeline with this project, and I just want to know if what I want to do is at all possible, or if I need to start looking into other options.

    Thanks

    October 17, 2014 at 1:05 am #325
    admin
    Keymaster

    I don’t have a 100% clear picture of what you are trying to do so my answer is not going to be the best unfortunately. I am assuming that you will have the user’s email address from the other plugin? You should be able to retrieve the affiliate’s profile/record using the email address (instead of AID). Then get the strRefKey of that affiliate. Won’t that work?

    October 17, 2014 at 6:59 am #326
    christianf3
    Participant

    Well, the affiliate’s email address in PM Pro/wordpress may be totally different from the one the user used to signup to become an affiliate.

    I’m wondering how to get the AID of the referring affiliate from the database by using the refkey value passed in the url or via a cookie during signup?

    Then, how can I use the AID to get the userId of the affiliate? (I need this user id to get the user’s invite code from the wordpress usermeta table)

    Lastly, how can I get the AID of the affiliate using their userId? (This will allow me to use the code you provided to get and assign the refkey)

    I just need to know how to pull this data from the database, and I can take care of the rest.

    October 19, 2014 at 10:48 am #330
    admin
    Keymaster

    From the WP user ID, you can get the email address of that user. Using that email address, you can query the affiliate plugin’s table and get the AID.

    October 20, 2014 at 7:04 am #333
    christianf3
    Participant

    Thanks for the response. I’m still a bit unclear. . .

    Can you give me some example code that will query the affiliate’s table to get the AID using that user’s email?

    Once I know the right way to query the affiliate’s table, I’ll be able to change it to get the other values I need.

    October 21, 2014 at 1:16 am #336
    admin
    Keymaster

    Here is an exmaple snippet of code to retrieve an affiliates profile using email then getting the AID value:

    
    global $wpdb
    $affiliate_email = "[email protected]";//TODO - enter the affiliate's email
    $tabl_name = WPAM_AFFILIATES_TBL;
    $affiliate = $wpdb->get_row("SELECT * FROM $tabl_name WHERE email = '$affiliate_email'");
    if($affiliate){
        //Found an affiliate record
        $aid = $affiliate->affiliateId;//Value of AID
    }
    
    October 24, 2014 at 2:11 pm #343
    christianf3
    Participant

    Thanks, that’s EXACTLY what I needed!

    So far, I’m halfway finished. (The affiliate’s invite code is automatically added into the PM Pro registration form field when an affiliate’s cookie is detected)

    Now I’m working on the other half. (Using only the invite code to set the affiliate cookie) I’ve finished writing the code that pulls the affiliate record based on the invite code entered. Now I just need a way to set the affiliate cookie.

    Can you please provide me with a code snippet that will allow me to set the affiliate cookie?

    Thanks again

  • Author
    Posts
Viewing 15 posts - 1 through 15 (of 17 total)
1 2 →
  • You must be logged in to reply to this topic.
Log In

Featured Addons

  • WooCommerce Coupon Tracking
  • WooCommerce Product Specific Commission

Copyright © 2026 | WordPress Affiliate Manager | Privacy Policy