Getting Product Multiples Using getActiveProductIds?

Discussion in 'Integration' started by chwoolgar, Jan 21, 2020.

  1. chwoolgar

    chwoolgar aMember Pro Customer

    Joined:
    Dec 5, 2012
    Messages:
    14
    How can I check for multiples of a product ID with the API?

    E.g.
    Code:
    require_once '....../bootstrap.php';
    $user = Am_Di::getInstance()->userTable->findFirstBy(array('login' => $username));
    $active_subs = $user->getActiveProductIds();
    print_r($active_subs);
    This would show E.g.
    Array ( [0] => 9 [1] => 39 [2] => 71 )

    Whereas the user actually has multiples of product ID 39.

    Is there a way to get active product ID's, or to check how many multiples a user has of a specific product ID?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You need to query database to get this info. Here is example code:
    PHP:
    $qty Am_Di::getInstance()->db->selectCell(
        
    "SELECT SUM(qty) FROM ?_access WHERE product_id=? AND user_id=? AND begin_date<=? AND expire_date>=?",
        
    39//pid
        
    $user->pk(), //uid
        
    sqlDate('now'),
        
    sqlDate('now')
    );
    Please note result can be 0 - it means user has not active access to this product (with id 39).
    chwoolgar likes this.
  3. chwoolgar

    chwoolgar aMember Pro Customer

    Joined:
    Dec 5, 2012
    Messages:
    14
    Very helpful, works perfectly. Many thanks :)
    caesar likes this.
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You are welcome!
    chwoolgar likes this.

Share This Page