API for getting active products for a single user

Discussion in 'aMember Pro v.4' started by apetersen, Apr 29, 2012.

  1. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Is it me that can't find the documentation or is there really no function for getting the active subscriptions (product number) for a single user?

    Some help is greatly appreciated! :)
  2. spicyniknaks

    spicyniknaks New Member

    Joined:
    Feb 27, 2012
    Messages:
    20
    There is a function in the user controller to get active product Id's for a user ( getActiveProductIds() )

    There are a few different ways of getting the user object, either directly or via the invoice etc. I don't have a fully structured example I can post here, but I have taken a snippet out of some of my code which will hopefully demonstrate how you can use this. Search the forum for info on how to get user details and then you can use this code from here.

    The code below is my code which is getting the invoice from a user id and then I get the User object from that invoice. If you have a userId then place it where the variable $userIdToFind is. The $activeProductArray variable will contain an array of all of the active products for that user.

    I hope this helps.

    PHP:
    $invoices Am_Di::getInstance()->invoiceTable->findByUserId($userIdToFind);
     
    $user $invoice->getUser();
     
    $activeProductArray $user->getActiveProductIds();
  3. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Hey, I appreciate your help. When including Am_Di I get the following error:
    "Fatal error: Class 'sfServiceContainerBuilder' not found in" message

    What am I missing?
  4. spicyniknaks

    spicyniknaks New Member

    Joined:
    Feb 27, 2012
    Messages:
    20
    Hi apetersen,

    These were just selected lines from a plugin I was using. It is not a complete script. I was just showing you the commands you can use. Below is a small example of a working script to get the active products for a user and also some code to show you how you can loop through invoices and products etc.

    There are several different ways to access the data you are after, this is just an example of one way that I am using.

    I think you should be able to put this code into your site.php and call it by going to url http://yourdomain.com/amember/?xx=1

    PHP:
    <?php
        
    /////////////////////////////////////////////////////////////
        // Example of code to get active products
        // //////////////////////////////////////////////////////////
       
        // If you are calling this script outside of site.php or outside of a plugin then you will need to include Lite.php
        // require_once '/path/to/your/amember/library/Am/Lite.php';
     
        
    if ($_GET['xx'] == 1) {
     
            
    $lite Am_Lite::getInstance();
     
            
    // Get all invoices for user
            
    $invoices Am_Di::getInstance()->invoiceTable->findByUserId(20);        // Insert user id inside findByUserId() function
     
            
    foreach ($invoices as $invoice)
            {
                
    $user $invoice->getUser();
     
                echo 
    "INVOICE<BR>--------------------<BR>";
                foreach (
    $invoice->getProducts() as $product)
                {
                    echo 
    "Product pk = " $product->pk() . "<BR>\n";
                }
     
                
    $activeProductArray $user->getActiveProductIds();
     
                print 
    "Active products for user {$user->id} = ";
                
    print_r($activeProductArray);
                print 
    "<BR>";
            }
        }
     
    ?>
    Hopefully this should get you started.
  5. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Hey,
    When I'm not including the Di.php in my script I get the following error message:
    "Fatal error: Class 'Am_Di' not found in ..."

    When I include Di.php I still get the:
    "Fatal error: Class 'sfServiceContainerBuilder' not found in ..." message

    So what am I doing wrong or missing?
  6. spicyniknaks

    spicyniknaks New Member

    Joined:
    Feb 27, 2012
    Messages:
    20
    Have you tried the above script in the site.php file on its own with nothing else to see if it works. Remember to set the user id to a valid id inside the findByUserId function.
  7. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Also remember depending on where this script is located, and called, you might need to uncomment, and adjust the following line to suit your actual site:
    PHP:
        // require_once '/path/to/your/amember/library/Am/Lite.php';
  8. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Hi, I'm back at it.

    Yes, I added the include for the Lite.php and added a valid user_id in the findByUserId function. I still get the error message:

    "Fatal error: Class 'Am_Di' not found in... "

    Aren't I supposed to include something else?
  9. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    I've created a file containing only the code above from spicyniknaks
  10. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Hoping someone can help me. I am certain that I am missing something since it cannot be that difficult to check what products a user are/is subscribing to? Really?

    I created a separate .php file containing the following code as per spicyniknaks recommendations:

    <?php

    /////////////////////////////////////////////////////////////
    // Example of code to get active products
    // //////////////////////////////////////////////////////////

    require_once '../amember4/library/Am/Lite.php';

    if ($_GET['xx'] == 1) {

    $lite = Am_Lite::getInstance();

    // Get all invoices for user
    $invoices = Am_Di::getInstance()->invoiceTable->findByUserId(139); // Insert user id inside findByUserId() function

    foreach ($invoices as $invoice)
    {
    $user = $invoice->getUser();

    echo "INVOICE<BR>--------------------<BR>";
    foreach ($invoice->getProducts() as $product)
    {
    echo "Product pk = " . $product->pk() . "<BR>\n";
    }

    $activeProductArray = $user->getActiveProductIds();

    print "Active products for user {$user->id} = ";
    print_r($activeProductArray);
    print "<BR>";
    }
    }
    ?>


    Accessing this the above page shows the following error still:
    Fatal error: Class 'Am_Di' not found in <path> /getproducts.php on line 16

    Do I need to include this Am_Di as well?
  11. apetersen

    apetersen New Member

    Joined:
    Sep 10, 2008
    Messages:
    13
    Maybe I should mention what I am trying to do:

    The user is subscribing to web content and they can subscribe to different levels of content so when they access a page I need to check what product/subscription they have. Furthermore they can have multiple products because they would sign up for a trial and then later a full version which would leave them with multiple products (at least until the trial expires). The trial version is limited content so if they sign up for a full version obviously they need access full content.
  12. rtcseo

    rtcseo New Member

    Joined:
    Jun 22, 2011
    Messages:
    6
    I am trying to do the same thing. This should be so easy...

    I need to get product IDS because a member can be subscribed to 1,2, or 3 products.

    Thus, I need to be able to do an (if productid=x, then do this), if productid = X AND product id=Y) then do this.

    This should not be that hard.

    Apetersen, have you found a solution yet that just might not be documented here?
  13. rtcseo

    rtcseo New Member

    Joined:
    Jun 22, 2011
    Messages:
    6
    Apetersen, I think I may have found a solution for you. I have just been testing all of the various APIs on this page http://www.amember.com/docs/API/Lite ...and found this that seems to answer my question and perhaps yours.

    There is a function called: Am_Lite::getInstance()->haveSubscriptions($search)

    Now, if we replace the $search with our product #, it will give us a true (=1) if the user has a subscription. So, we can say something like this: $product2user =Am_Lite::getInstance()->haveSubscriptions(2);

    If $product2user has access, it will be = 1.

    You obviously will want to make sure that the user is logged on when you run this. To accomplish this, in the beginning of my file, I use these two lines:

    require_once '/home/ugbad/public_html/amember/library/Am/Lite.php';
    Am_Lite::getInstance()->checkAccess(array(2,3));

    Then, in your php files, you can say something like this:

    if ($product2user == 1) {
    blah blah blah
    }

    I hope this helps. Boy, you sure have to figure a lot aMember out for yourself if you want to do anything beyond the basics...
  14. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    If you include full API, you can do the following:

    PHP:
    require_once 'amember/bootstrap.php';
    // will throw exception if user is not logged in!
    // use Am_Di::getInstance()->auth->getUserId() > 0 to check if user logged-id or not
    Am_Di::getInstance()->user->getActiveProducts();
    // or
    Am_Di::getInstance()->user->getActiveProductsExpiration();
    http://www.amember.com/api/Am_Record/User.html
  15. regis

    regis aMember Pro Customer

    Joined:
    Aug 29, 2006
    Messages:
    40
    Is there a way to pull more member info? Such as company name, address, phone number, etc.. for auto filling forms, etc.?

    Thanks
  16. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279

Share This Page