I want all users who sign up through the popup to be automatically assigned to a default free membership plan. How can I implement this?
Hi,
If you’re familiar with coding, please check how a free membership is assigned here Awesome Screenshot You can try replicating the same code, creating a membership on hivepress/v1/models/user/register action.
Another possible solution is more simple but requires some testing (it may interrupt other redirects, e.g. when user tries to add a listing and expects to return to the next step after the registration). You can set the redirect URL to /select-plan/123, where 123 is the free plan ID, and set it as redirect for the form via the hivepress/v1/forms/user_register hook.
Hope this helps
Thanks @ihor ! I tried the following code:
function default_membership( $user_id ) {
$default_free_plan_id = 123; //Membership Plan Post ID, This needs to be edited
$plan = \HivePress\Models\Membership_Plan::query()->get_by_id( $default_free_plan_id );
if ( ! $plan || $plan->get_status() !== 'publish' || $plan->get_price() > 0 ) {
return;
}
$membership = ( new \HivePress\Models\Membership() )->fill(
array_merge(
$plan->serialize(),
[
'status' => 'publish',
'user' => $user_id,
'plan' => $plan->get_id(),
'default' => true,
]
)
)->save();
// Send an email notification (optional)
$user = get_user_by( 'id', $user_id );
if ( $user && $membership ) {
( new \HivePress\Emails\Membership_Activate(
[
'recipient' => $user->user_email,
'tokens' => [
'user' => $user,
'membership' => $membership,
'user_name' => $user->display_name,
'membership_plan' => $plan->get_name(),
'memberships_url' => hivepress()->router->get_url( 'memberships_view_page' ),
],
]
) )->send();
}
}
add_action( 'hivepress/v1/models/user/register', 'default_membership', 10, 1 );
It works, but there is an issue. This code assigns the user default membership even before the user has verified its email id.
Now since the user is assigned membership immediately after registration, it somehow bypasses the email verification during the login.
I have removed this code for now.
Shall I directly modify the redirect_user_email_verify_page function in the controller, like update the membership after the delete email key? Like update membership directly & email the user as well? I tried a few things, but a bit of guidance will help…
Here’s the solution (Use with caution, do not modify core files and if you use this code, please test it at your end):
Go to the controller in hivepress plugin:
/hivepress/includes/controllers/class-user.php
Find the function that redirects user to verification page redirect_user_email_verify_page
In that function add the following action just before return false in the last, something in between these lines:
}
// Check if the email verification key was present and removed
$email_verification_key_exists = get_user_meta( $user->ID, 'hp_email_verify_key', true );
// Trigger the custom hook only if the email verification key no longer exists.
if ( ! $email_verification_key_exists ) {
do_action( 'add_default_membership', $user->ID );
}
return false;
}
Now you do the remaining part in functions.php, add following code:
add_action( 'add_default_membership', 'assign_membership_after_email_verification', 10, 1 );
/**
* Assign a default free membership plan to the user after email verification.
*
* @param int $user_id User ID.
*/
function assign_membership_after_email_verification( $user_id ) {
// // Ensure HivePress Membership is available.
if ( ! class_exists( '\HivePress\Models\Membership' ) || ! class_exists( '\HivePress\Models\Membership_Plan' ) ) {
return;
}
// Define the default free plan ID.
$default_free_plan_id = 1; // Change to your default membership plan ID.
// Validate the membership plan.
$plan = \HivePress\Models\Membership_Plan::query()->get_by_id( $default_free_plan_id );
if ( ! $plan || $plan->get_status() !== 'publish' || $plan->get_price() > 0 ) {
return;
}
// Check if the user already has an existing membership for the plan.
$existing_membership = \HivePress\Models\Membership::query()
->filter([
'user' => $user_id,
'plan' => $default_free_plan_id
])
->get_first(); // Get the first result if any
if ( $existing_membership ) {
return; // User already has the default membership plan, do not assign it again.
}
// Create the membership
$membership = ( new \HivePress\Models\Membership() )->fill(
array_merge(
$plan->serialize(),
[
'status' => 'publish',
'user' => $user_id,
'plan' => $plan->get_id(),
'default' => true,
]
)
)->save();
// (Optional) Send an email notification about the membership activation. This will send email if you have email already activated in your email setting.
$user = get_user_by( 'id', $user_id );
if ( $user && $membership ) {
( new \HivePress\Emails\Membership_Activate(
[
'recipient' => $user->user_email,
'tokens' => [
'user' => $user,
'membership' => $membership,
'user_name' => $user->display_name,
'membership_plan' => $plan->get_name(),
'memberships_url' => hivepress()->router->get_url( 'memberships_view_page' ),
],
]
) )->send();
}
}
I hope it helps someone in need.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
Hey,
Would it be possible to provide a code snippet to automatically assign a free Membership to everyone upon registration?
Cheers,
Chris ![]()
Hi,
Please check the solution in this topic.
I hope it helps
Thanks, @Andrii - will test this out later today! ![]()
Hi Guys I have the exact same requirement, to auto register new users as free members. Is the above something you can add as a snippet?. Does it work.
For anyone looking for the same solution, the current reliable code snippet you can use is below:
add_action(
'hivepress/v1/emails/user_register/send',
function( $email ) {
// Set plan ID.
$plan_id = 123;
// Get user.
$user = hivepress()->helper->get_array_value( $email->get_tokens(), 'user' );
if ( ! $user ) {
return;
}
// Get plan.
$plan = \HivePress\Models\Membership_Plan::query()->get_by_id( $plan_id );
if ( ! $plan ) {
return;
}
// Add membership.
hivepress()->membership->add_membership( $user->get_id(), $plan );
}
);
We’ll also add this option to the next membership update, it will be possible to select the default membership for all users in HivePress/Settings/Memberships.
Hope this helps