Possible To Insert Product With AM:DI

Discussion in 'aMember Pro v.4' started by chwoolgar, May 14, 2020.

  1. chwoolgar

    chwoolgar New Member

    Joined:
    Dec 5, 2012
    Messages:
    14
    Is it possible to add a product to an account via the API?

    E.g.
    $user = Am_Di::getInstance()->userTable->findFirstBy(array('login' => $username));
    $active_subs = $user->getActiveProductIds();
    if(!in_array(ProductID,$active_subs)){
    // Something here to add product
    $user->insertActiveProduct(ProductID); // Sample
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,112
    Hello,

    Yes, it is possible:

    PHP:
    Am_Di::getInstance()->accessTable->insert([
       
    'user_id' => $user->pk(),
       
    'product_id' => $ProductID,
       
    'qty' => 1,
       
    'begin_date' => sqlDate('now'),
       
    'expire_date' => sqlDate('+1year'),
    ]);
    chwoolgar likes this.
  3. chwoolgar

    chwoolgar New Member

    Joined:
    Dec 5, 2012
    Messages:
    14
    Awesome, many thanks :)
  4. chwoolgar

    chwoolgar New Member

    Joined:
    Dec 5, 2012
    Messages:
    14
    Also, is there any other API calls required after this? As it seems to add the product, and shows as active, yet when searching for that same user it shows not active even though the sub is active in the user account.
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,112
    Please change code to this one:
    PHP:
    $access Am_Di::getInstance()->accessRecord;
    $access->setForInsert([
        
    'user_id' => $user->pk(),
        
    'product_id' => $ProductID,
        
    'qty' => 1,
        
    'begin_date' => sqlDate('now'),
        
    'expire_date' => sqlDate('+1year'),
    ]);
    $access->save();
    or below insert via table add this code:
    PHP:
    $user->checkSubscriptions(true);
    chwoolgar likes this.
  6. chwoolgar

    chwoolgar New Member

    Joined:
    Dec 5, 2012
    Messages:
    14
    Thanks, this works well :)

Share This Page