Membership Code: Add to a PHP file and then call it via your functions file or again, use a code snippets plugin.
/**
WooCommerce Course Access System for Bricks Builder
Features:
Creates WordPress accounts on completed purchases
Checks course access permissions
Integrates with Bricks Builder dynamic data
Handles clean username generation
Sends password setup emails
*/
// Security check
if (!defined(âABSPATHâ)) {
exit;
}
/**
Check if user has purchased a specific product
@param int $product_id The product ID to check
@return bool True if purchased, false otherwise
*/
function user_has_bought_product($product_id) {
if (!is_user_logged_in()) return false;
$user_id = get_current_user_id();
// First check user meta for cached purchases
$purchased_courses = get_user_meta($user_id, âpurchased_coursesâ, false);
if (in_array($product_id, $purchased_courses)) {
return true;
}
// If not found in meta, check order history
return check_orders_for_product($user_id, $product_id);
}
/**
Check order history for specific product purchase
@param int $user_id WordPress user ID
@param int $product_id WooCommerce product ID
@return bool True if found in orders
*/
function check_orders_for_product($user_id, $product_id) {
$orders = wc_get_orders([
âcustomer_idâ => $user_id,
âstatusâ => âcompletedâ,
âlimitâ => -1,
âreturnâ => âidsâ,
]);
foreach ($orders as $order_id) {
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
if ($item->get_product_id() == $product_id) {
// Cache this purchase in user meta
add_user_meta($user_id, âpurchased_coursesâ, $product_id);
return true;
}
}
}
return false;
}
/**
Register Bricks Builder dynamic data function
*/
add_filter(âbricks/custom/dynamic_dataâ, function($tags) {
$tags[âuser_has_bought_productâ] = [
âlabelâ => âCheck Product Purchaseâ,
âcallbackâ => function($attributes) {
if (empty($attributes[âproduct_idâ])) {
error_log(âMissing product_id parameter in user_has_bought_productâ);
return false;
}
return user_has_bought_product(intval($attributes[âproduct_idâ]));
},
âparamsâ => [
âproduct_idâ => [
âlabelâ => âProduct IDâ,
âtypeâ => ânumberâ,
ârequiredâ => true
]
]
];
return $tags;
});
/**
Create WordPress user when WooCommerce order is completed
*/
add_action(âwoocommerce_order_status_completedâ, âcreate_wp_user_on_order_completionâ, 10, 2);
function create_wp_user_on_order_completion($order_id, $order) {
// Get the customer email from the order
$customer_email = $order->get_billing_email();
// Check if a user already exists with this email
if (email_exists($customer_email)) {
return; // User already exists, do nothing
}
// Get customer details from the order
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
// Generate clean username (firstname.lastname)
$username = generate_clean_username($first_name, $last_name, $customer_email);
// Generate a random password (will be reset via email)
$password = wp_generate_password();
// Create the new user with default role
$user_id = wp_create_user($username, $password, $customer_email);
if (is_wp_error($user_id)) {
error_log('Failed to create user for order ' . $order_id . ': ' . $user_id->get_error_message());
return;
}
// Assign course student role
$user = new WP_User($user_id);
$user->set_role('customer'); // Change to your preferred role
// Update user meta with names
wp_update_user([
'ID' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => $first_name . ' ' . $last_name
]);
// Store billing information
update_user_meta($user_id, 'billing_first_name', $first_name);
update_user_meta($user_id, 'billing_last_name', $last_name);
update_user_meta($user_id, 'billing_email', $customer_email);
update_user_meta($user_id, 'billing_phone', $order->get_billing_phone());
update_user_meta($user_id, 'billing_address_1', $order->get_billing_address_1());
update_user_meta($user_id, 'billing_city', $order->get_billing_city());
update_user_meta($user_id, 'billing_postcode', $order->get_billing_postcode());
update_user_meta($user_id, 'billing_country', $order->get_billing_country());
// Track purchased courses
foreach ($order->get_items() as $item) {
add_user_meta($user_id, 'purchased_courses', $item->get_product_id());
}
// Send password reset email
send_password_reset_email($user_id);
}
/**
Generate clean username (firstname.lastname)
Handles duplicates by adding incrementing number
*/
function generate_clean_username($first_name, $last_name, $email = ââ) {
// Sanitize names - remove accents, special chars, etc.
$clean_first = sanitize_title($first_name);
$clean_last = sanitize_title($last_name);
// Remove any remaining special characters
$clean_first = preg_replace(â/[^a-z0-9]/â, ââ, $clean_first);
$clean_last = preg_replace(â/[^a-z0-9]/â, ââ, $clean_last);
// If either name is empty after cleaning, use email prefix as fallback
if (empty($clean_first) || empty($clean_last)) {
return sanitize_user(current(explode(â@â, $email)), true);
}
// Create base username
$base_username = $clean_first . â.â . $clean_last;
$username = $base_username;
$counter = 1;
// Check if username exists and increment if needed
while (username_exists($username)) {
$username = $base_username . $counter;
$counter++;
}
return $username;
}
/**
Send password reset email to new user
*/
function send_password_reset_email($user_id) {
$user = get_userdata($user_id);
if (!$user) return;
$key = get_password_reset_key($user);
if (is_wp_error($key)) return;
$reset_url = network_site_url(âwp-login.php?action=rp&key=$key&login=â . rawurlencode($user->user_login), âloginâ);
$subject = sprintf(__(âSet your password for %sâ), get_bloginfo(ânameâ));
$message = sprintf(__(âHello %s,â), $user->first_name) . â\r\n\r\nâ;
$message .= __(âYour account has been created.â) . â\r\n\r\nâ;
$message .= __(âTo set your password, please click the following link:â) . â\r\n\r\nâ;
$message .= $reset_url . â\r\n\r\nâ;
$message .= __(âThis link will expire in 24 hours.â) . â\r\n\r\nâ;
$message .= __(âIf you didnât request this, please ignore this email.â) . â\r\nâ;
wp_mail($user->user_email, $subject, $message);
}