Duplicate Snippet
Embed Snippet on Your Site
WP Simple Pay: Create WordPress User After Payment
Creates a new WordPress user when a subscription is started or a one-time payment is made.
Code Preview
php
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Do you have a version of this snippet that will assign a different role based on the price the user paid? For example if there are 3 prices, each associated with a membership level.
Hello James,
You can access the amount that was paid with the following:
“`
if ( ‘invoice’ === $object->type ) {
$amount = $object->amount_paid;
} else {
$amount = $object->amount;
}
“`
You can then perform different actions based on the amount paid. I hope that helps!
This seems to be working, sort of, however, it’s ignoring my conditions and just assigned the first $user_id it’s encountering, if though it’s contained with a conditional statement. Do you seen any obvious issue here?
if ($amount = “35.00”) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘subscriber’,
‘user_pass’ => null,
) );
} elseif ($amount = “100.00”) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘first_tracks’,
‘user_pass’ => null,
) );
} elseif ($amount = “500.00”) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘hut_hero’,
‘user_pass’ => null,
) );
}
Sorry, I pasted incorrect code since I am checking to see if the variable equals a value, but this still doesn’t behave as expected:
if ($amount == ’35’) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘subscriber’,
‘user_pass’ => null,
) );
} elseif ($amount == ‘100’) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘first_tracks’,
‘user_pass’ => null,
) );
} elseif ($amount == ‘500’) {
$user_id = wp_insert_user( array(
‘user_email’ => $email_address,
‘user_login’ => $email_address,
‘nickname’ => $email_address,
‘role’ => ‘hut_hero’,
‘user_pass’ => null,
) );
}
After some go rounds, I reverted back to your original code with one change: I changed
‘role’ => ‘subscriber’,
to
‘role’ => ‘foundation’,
Then, after my test, it created a user with a role of Subscriber.
I thought perhaps it was because Subscriber was set as the default role, but changing that made no difference.
So, it’s ignoring my edits. Does that make sense? I am using the Members plugin from Memberpress to create my roles. Ideally I want to evaluate the amount and then assign a role based on the amount.
Thanks for any input you might have.
Note: the code has been updated to support role assignments, so anyone looking can ignore all my examples.