programmatically create transaction memberpress
Asked Answered
R

1

8

I found following snippet on github:

https://gist.github.com/supercleanse/8010675

    if(is_plugin_active('memberpress/memberpress.php')) {
      add_action( 'user_register', 'mp_auto_enroll' );
      //add_action( 'gform_user_registered', 'mp_auto_enroll', 10, 4 );

      function mp_auto_enroll($user_id, $user_config=array(), $entry='', $user_pass='') {
        $txn = new MeprTransaction();
        $txn->user_id = $user_id;
        $txn->product_id = 8; // TODO: Make sure you change this value to whatever the product_id will be
        $txn->trans_num  = uniqid();
        $txn->status     = MeprTransaction::$complete_str;
        $txn->gateway    = MeprTransaction::$free_gateway_str;
        $txn->expires_at = 0; // 0 = Lifetime, null = product default expiration
        $txn->store();
      }
    }

Above code adds only non-recurring transaction, how to add recurring transaction

I tried to add

 $txn->amount = 100;

 $txn->period_type="month";

its not working , any clues please

Ricky answered 15/7, 2016 at 18:7 Comment(1)
@supercleanse any idea please I see that you are in stackoverflow tooRicky
R
14

The following code worked , hope it helps somebody else too .

            $sub = new MeprSubscription();
            $sub->user_id = $user_ID;
            $sub->product_id = 123;
            $sub->price = 12.99;
            $sub->total = 12.99;
            $sub->period = 1;
            $sub->period_type = 'months';
            $sub->status = MeprSubscription::$active_str;
            $sub_id = $sub->store();

            $txn = new MeprTransaction();
            $txn->amount = 12.99;
            $txn->total = 12.99;
            $txn->user_id = $user_ID;
            $txn->product_id = 123;
            $txn->status = MeprTransaction::$complete_str;
            $txn->txn_type = MeprTransaction::$payment_str;
            $txn->gateway = 'manual';
            $txn->expires_at = gmdate('Y-m-d 23:59:59', (time() + MeprUtils::months(1)));
            $txn->subscription_id = $sub_id;
            $txn->store();
Ricky answered 18/8, 2016 at 2:50 Comment(4)
Saved my day! works for me too.Atlas
Big thanks @govindak!! For anyone wondering, this is how you can programmatically assign a Memberpress membership to a WordPress user. product_id is the membership id. For free or lifetime memberships, you only need to create a new MeprTransaction(), while recurring memberships need both MeprTransaction() and MeprSubscription() to be created. Have a look at the ` wp_mepr_subscriptions` and ` wp_mepr_subscriptions` database tables to see how the data is structured and stored.Coaming
Github gist to do the same thing: gist.github.com/andrewlimaza/5bd2da7bea6db2f6e8ff46cced0a363bLots
Just for the record if you need to handle the hour of the expiry (I need it for testing purpose) this is the way I found to change the 23:59:59 (no matter what you set in expires_at) gist.github.com/cartpauj/9308f60e7fd51939e6809b2ce3a685f4Rompers

© 2022 - 2024 — McMap. All rights reserved.