Skip to content

Instantly share code, notes, and snippets.

@supercleanse
Last active October 11, 2022 04:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save supercleanse/8010675 to your computer and use it in GitHub Desktop.
Save supercleanse/8010675 to your computer and use it in GitHub Desktop.
Automatically create a lifetime transaction for a specific product each time a user registers.
<?php
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();
}
}
@govindak
Copy link

how to create recurring transcation with above codes pls

@cartpauj
Copy link

You'd have to create a MeprSubscription object and store it. Then assign the first Transaction to it afterwards.

$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();

Then every month, you'd need to programatically add a new Transaction with the same $sub_id for that user.

@govindak
Copy link

okay thank you , worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment