Chia sẻ code Tự động tạo tài khoản cho khách hàng và gửi thông báo sau khi hoàn tất đơn hàng trong WooCommerce WordPress
Bạn đang làm trang website bán hàng bằng WooCommerce WordPress và muốn khi khách hàng mua sản phẩm sẽ tạo tài khoản tự động và gửi về mail cho khách hàng tài khoản đã tạo, Dưới đây Vareno giới thiệu bạn đoạn code hay, dễ sử dụng
Với các tính năng của đoạn code như:
- Tự động tạo tài khoản sau khi checkout
- Gửi email HTML đẹp với thông tin đăng nhập
- Hiển thị thông báo trên trang Thank You
- Tránh trùng lặp username và email
- Tương thích Flatsome – màu sắc phù hợp với theme
/**
* Tự động tạo tài khoản cho khách hàng sau khi hoàn tất đơn hàng
* Tương thích với theme Flatsome và WooCommerce
*/
// Tạo tài khoản sau khi đơn hàng được tạo
function auto_create_account_after_checkout($order_id) {
// Tránh chạy nhiều lần
if (!$order_id || get_post_meta($order_id, '_account_created', true)) {
return;
}
$order = wc_get_order($order_id);
if (!$order) {
return;
}
// Kiểm tra xem khách hàng đã có tài khoản chưa
$customer_id = $order->get_customer_id();
if (empty($customer_id)) {
$email = $order->get_billing_email();
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
// Tạo username từ email
$username = sanitize_user(explode('@', $email)[0]);
// Đảm bảo username là duy nhất
$original_username = $username;
$counter = 1;
while (username_exists($username)) {
$username = $original_username . $counter;
$counter++;
}
// Kiểm tra email đã tồn tại chưa
if (email_exists($email)) {
// Nếu email đã tồn tại, gán user đó cho đơn hàng
$existing_user = get_user_by('email', $email);
if ($existing_user) {
$order->set_customer_id($existing_user->ID);
$order->save();
}
return;
}
// Tạo mật khẩu ngẫu nhiên
$random_password = wp_generate_password(12, false);
// Tạo user mới
$user_id = wp_create_user($username, $random_password, $email);
if (!is_wp_error($user_id)) {
// Cập nhật thông tin user
wp_update_user(array(
'ID' => $user_id,
'role' => 'customer',
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => $first_name . ' ' . $last_name
));
// Gán user cho đơn hàng
$order->set_customer_id($user_id);
$order->save();
// Đánh dấu đã tạo tài khoản
update_post_meta($order_id, '_account_created', true);
// Gửi email thông báo cho khách hàng
send_account_notification_email($email, $username, $random_password, $first_name);
}
}
}
add_action('woocommerce_checkout_order_processed', 'auto_create_account_after_checkout', 10, 1);
/**
* Gửi email thông báo tài khoản mới
*/
function send_account_notification_email($email, $username, $password, $first_name) {
$site_name = get_bloginfo('name');
$site_url = home_url();
$login_url = wc_get_page_permalink('myaccount');
$subject = "[$site_name] Tài khoản của bạn đã được tạo";
$message = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: #446084; color: white; padding: 20px; text-align: center; }
.content { padding: 30px; background: #f9f9f9; }
.credentials { background: white; padding: 20px; margin: 20px 0; border-left: 4px solid #446084; }
.button { display: inline-block; padding: 12px 30px; background: #446084; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; }
.footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
</style>
</head>
<body>
<div class='container'>
<div class='header'>
<h1>$site_name</h1>
</div>
<div class='content'>
<p>Xin chào <strong>$first_name</strong>,</p>
<p>Cảm ơn bạn đã mua hàng tại <strong>$site_name</strong>!</p>
<p>Chúng tôi đã tạo tài khoản cho bạn để tiện theo dõi đơn hàng và mua sắm trong tương lai.</p>
<div class='credentials'>
<h3>Thông tin đăng nhập:</h3>
<p><strong>Tên đăng nhập:</strong> $username</p>
<p><strong>Mật khẩu:</strong> $password</p>
<p><strong>Link đăng nhập:</strong> <a href='$login_url'>$login_url</a></p>
</div>
<p><em>Lưu ý: Vì lý do bảo mật, vui lòng đổi mật khẩu sau khi đăng nhập lần đầu.</em></p>
<center>
<a href='$login_url' class='button'>Đăng nhập ngay</a>
</center>
</div>
<div class='footer'>
<p>© " . date('Y') . " $site_name. All rights reserved.</p>
<p><a href='$site_url'>$site_url</a></p>
</div>
</div>
</body>
</html>
";
// Headers cho email HTML
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . $site_name . ' <' . get_option('admin_email') . '>'
);
// Gửi email
wp_mail($email, $subject, $message, $headers);
}
/**
* Hiển thị thông báo trên trang Thank You (tương thích Flatsome)
*/
function display_account_created_notice($order_id) {
if (get_post_meta($order_id, '_account_created', true)) {
$order = wc_get_order($order_id);
$email = $order->get_billing_email();
echo '<div class="woocommerce-message" style="margin-bottom: 20px; padding: 15px; background: #dff0d8; border: 1px solid #d6e9c6; border-radius: 4px;">
<strong>🎉 Tài khoản đã được tạo!</strong><br>
Thông tin đăng nhập đã được gửi đến email: <strong>' . esc_html($email) . '</strong><br>
<small>Vui lòng kiểm tra hộp thư (hoặc thư mục Spam) để nhận thông tin.</small>
</div>';
}
}
add_action('woocommerce_thankyou', 'display_account_created_notice', 5);
Cách sử dụng
Bạn chỉ cần coppy đoạn code trên và thêm code vào file functions.php của child theme Flatsome
Sau đó lưu lại và xem kết quả.
Xem thêm:
- [Share Code] Chuyển 0đ thành chữ “Liên hệ” trong Woocommerce WordPress
- Thiết Kế Website Bán Hàng WordPress Chuyên Nghiệp
- Hướng dẫn tạo bài viết liên quan đẹp mắt trong theme Flatsome WordPress
- [Share Code] Chia sẻ code ẩn hiện text với hiệu ứng hover mượt mà trong website WordPress
- Những Yếu Tố Cần Thiết Để Có Bài Viết Chuẩn SEO Google
![[Share Code] Tự động tạo tài khoản cho khách hàng và gửi thông báo sau khi hoàn tất đơn hàng trong WooCommerce WordPress 1 woocomerce plugin - ThemeVareno.com](https://themevareno.com/wp-content/uploads/2025/12/woocomerce-plugin.png)
![[SEO] Khắc phục lỗi Google không index tốt với các sản phẩm không có giá trong Woocomerce 2 seo price woocomerce 1 - ThemeVareno.com](https://themevareno.com/wp-content/uploads/2025/12/seo-price-woocomerce-1.jpg)
![[Share Code] Chia sẻ code ẩn hiện text với hiệu ứng hover mượt mà trong website Wordpress 3 an hien text 2 - ThemeVareno.com](https://themevareno.com/wp-content/uploads/2025/12/an-hien-text-2.jpg)
![[Share Code] Chia sẻ code xóa các hình không sử dụng trong website WordPress 4 Remove Default WordPress Image Sizes - ThemeVareno.com](https://themevareno.com/wp-content/uploads/2025/12/Remove-Default-WordPress-Image-Sizes.png)
![[Share Code] Chỉnh sửa fields trong trang checkout của Woocommerce 5 checkout cua woocommerce 2 - ThemeVareno.com](https://themevareno.com/wp-content/uploads/2025/12/checkout-cua-woocommerce-2.jpg)
