Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Prestashop » Tạo email thông báo khi có user đăng ký mới – Prestashop

Tạo email thông báo khi có user đăng ký mới – Prestashop

Thứ Sáu, 06/02/2015 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Trong bài học này chúng ta sẽ biết cách kích hoạt email thông báo (email notifications) khi có khách hàng đăng ký vào website bán hàng Prestashop.
Chúng ta sẽ tích hợp vào module đăng ký user, bạn sẽ cần thêm đoạn code snippet của chúng tôi để thông báo đến user khi họ đã đăng ký thành công tài khoản trên store của bạn. Và để làm điều này chúng ta sẽ sử dụng phương thức sendConfirmationMail trong file AuthController.php và tạo một mẫu nội dung email thông báo.

AuthController class
Chúng ta luôn sử dụng cách override thay vì sửa vào các file hệ thống đó là phương thức tốt nhất, vì lý do khi cập nhật Prestashop bạn sẽ dữ được nó. Do vậy, bạn tạo file mới đặt tên AuthController.php trong thư mục ‘override/controllers/front’. Bạn chèn vào nội dung dưới đây:

Class AuthController extends AuthControllerCore
{
 
}

Như mình đã đề cập ở trên, chúng ta cần kế thừa hàm sendConfirmationMail, và thực hiện override ở controller của chúng ta. Vì vậy hãy sao chép nó từ controller gốc và thêm vào lớp ‘AuthController’ như dưới đây, ví dụ với Prestashop 1.6.0.9 sẽ như thế này:

Class AuthController extends AuthControllerCore
{
    protected function sendConfirmationMail(Customer $customer)
    {
        if (!Configuration::get('PS_CUSTOMER_CREATION_EMAIL'))
            return true;
 
        return Mail::Send(
            $this->context->language->id,
            'account',
            Mail::l('Welcome!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email,
                '{passwd}' => Tools::getValue('passwd')),
            $customer->email,
            $customer->firstname.' '.$customer->lastname
        );
    }
     
}

Bạn cần Đảm bảo tính năng gửi email có hoạt động. Xóa file class_index.php có trong thư mục cache/. Sau đó thử đăng ký user mới và chuẩn bị một email nhận thông báo về user đăng ký này. Ok, chúng ta sẽ gửi notification cho admin (chủ shop), trước khi gọi lệnh return bạn thêm vào đoạn code sau:

Mail::Send(
            Configuration::get('PS_LANG_DEFAULT'),
            'account_for_admin',
            Mail::l('New client!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email
                ),
            Configuration::get('PS_SHOP_EMAIL'),
            Configuration::get('PS_SHOP_NAME')
        );

Giải thích:
Tôi đã copy hàm Mail::Send ở dòng return trên và sửa lại thông tin cần thiết. Đầu tiên, thay vì sử dụng customer language có thiết lập cho tài khoản khách hàng, chúng ta sử dụng cài đăt mặc định của store. Với cách này chúng ta không cần tạo nhiều hay dịch ngôn ngữ trong template. Tiếp đó, mình sử dụng ‘account_for_admin’ cho tên email template, và lấy thông tin của user vừa tạo như firstname, lastname, email.. cuối cùng chúng ta truyền vào tham số “to” là email của chủ shop, cũng như điền tiêu đề email. Bước tiếp theo chúng ta sẽ tạo email template.

Tạo mới email template

Thay vì viết template lại từ đầu, dành cho những người mới học và nhanh hơn mình sẽ copy một HTML template có trong Prestashop, ok mình lấy template ‘sau khi user đăng ký tài khoản thành công’ làm cơ sở. Truy cập folder emails/en (hoặc chọn một ngôn ngữ khác bạn muốn sử dụng trong template), nhân bản account.html và account.txt và đổi tên chúng thành ‘account_for_admin.html’, ‘account_for_admin.txt’. Mở lên file .txt nó sẽ có nội dung giống như thế này:

[{shop_url}] 
 
Hi {firstname} {lastname},
 
Thank you for creating a customer account at {shop_name}. 
 
Here are your login details:
 
E-MAIL ADDRESS: {email}
 
PASSWORD: {passwd}       
 
Important Security Tips:
 
* Always keep your account details safe.
 
* Never disclose your login details to anyone.
 
* Change your password regularly.
 
* Should you suspect someone is using your account illegally, please
notify us immediately.
 
You can now place orders on our shop: {shop_name}
[{shop_url}] 
 
{shop_name} [{shop_url}] powered by
PrestaShop(tm) [http://www.prestashop.com/] 

Sửa lại nội dung tùy thuộc vào nhu cầu của bạn, ví dụ:

[{shop_url}] 
 
Hi!
 
A new customer just signed up for an account at {shop_name}. 
 
Here are the details:
 
E-MAIL ADDRESS: {email}
 
NAME: {firstname} {lastname}

Sau đó sửa lại HTML template với file account_for_admin.html, có thể lấy nguyên HTML và thay đổi lại nội dung thông báo: Đổi:

<tr>
    <td align="center" class="titleblock" style="padding:7px 0">
        <font size="2" face="Open-sans, sans-serif" color="#555454">
            <span class="title" style="font-weight:500;font-size:28px;text-transform:uppercase;line-height:33px">Hi {firstname} {lastname},</span><br/>
            <span class="subtitle" style="font-weight:500;font-size:16px;text-transform:uppercase;line-height:25px">Thank you for creating a customer account at {shop_name}.</span>
        </font>
    </td>
</tr>

Thành:

<tr>
    <td align="center" class="titleblock" style="padding:7px 0">
        <font size="2" face="Open-sans, sans-serif" color="#555454">
            <span class="title" style="font-weight:500;font-size:28px;text-transform:uppercase;line-height:33px">Hi!</span><br/>
            <span class="subtitle" style="font-weight:500;font-size:16px;text-transform:uppercase;line-height:25px">A new customer just signed up for an account at {shop_name}.</span>
        </font>
    </td>
</tr>

Thay tiếp:

<tr>
    <td class="box" style="border:1px solid #D6D4D4;background-color:#f8f8f8;padding:7px 0">
        <table class="table" style="width:100%">
            <tr>
                <td width="10" style="padding:7px 0">&nbsp;</td>
                <td style="padding:7px 0">
                    <font size="2" face="Open-sans, sans-serif" color="#555454">
                        <p data-html-only="1" style="border-bottom:1px solid #D6D4D4;margin:3px 0 7px;text-transform:uppercase;font-weight:500;font-size:18px;padding-bottom:10px">
                            Your {shop_name} login details                      </p>
                        <span style="color:#777">
                            Here are your login details:<br /> 
                            <span style="color:#333"><strong>E-mail address: <a href="mailto:{email}" style="color:#337ff1">{email}</a></strong></span><br />
                            <span style="color:#333"><strong>Password:</strong></span> {passwd}
                        </span>
                    </font>
                </td>
                <td width="10" style="padding:7px 0">&nbsp;</td>
            </tr>
        </table>
    </td>
</tr>

Thành:

<tr>
    <td class="box" style="border:1px solid #D6D4D4;background-color:#f8f8f8;padding:7px 0">
        <table class="table" style="width:100%">
            <tr>
                <td width="10" style="padding:7px 0">&nbsp;</td>
                <td style="padding:7px 0">
                    <font size="2" face="Open-sans, sans-serif" color="#555454">
                        <p data-html-only="1" style="border-bottom:1px solid #D6D4D4;margin:3px 0 7px;text-transform:uppercase;font-weight:500;font-size:18px;padding-bottom:10px">
                            Your {shop_name} login details                      </p>
                        <span style="color:#777">
                            Here are your login details:<br /> 
                            <span style="color:#333"><strong>E-mail address: <a href="mailto:{email}" style="color:#337ff1">{email}</a></strong></span><br />
                            <span style="color:#333"><strong>Password:</strong></span> {passwd}
                        </span>
                    </font>
                </td>
                <td width="10" style="padding:7px 0">&nbsp;</td>
            </tr>
        </table>
    </td>
</tr>

Cuối cùng, file có nội dung hoàn chỉnh như sau:

<tr>
    <td class="space_footer" style="padding:0!important">&nbsp;</td>
</tr>
<tr>
    <td class="box" style="border:1px solid #D6D4D4;background-color:#f8f8f8;padding:7px 0">
        <table class="table" style="width:100%">
            <tr>
                <td width="10" style="padding:7px 0">&nbsp;</td>
                <td style="padding:7px 0">
                    <font size="2" face="Open-sans, sans-serif" color="#555454">
                        <p style="border-bottom:1px solid #D6D4D4;margin:3px 0 7px;text-transform:uppercase;font-weight:500;font-size:18px;padding-bottom:10px">Important Security Tips:</p>
                        <ol style="margin-bottom:0">
                            <li>Always keep your account details safe.</li>
                            <li>Never disclose your login details to anyone.</li>
                            <li>Change your password regularly.</li>
                            <li>Should you suspect someone is using your account illegally, please notify us immediately.</li>
                        </ol>
                    </font>
                </td>
                <td width="10" style="padding:7px 0">&nbsp;</td>
            </tr>
        </table>
    </td>
</tr>
<tr>
    <td class="space_footer" style="padding:0!important">&nbsp;</td>
</tr>
<tr>
    <td class="linkbelow" style="padding:7px 0">
        <font size="2" face="Open-sans, sans-serif" color="#555454">
            <span>You can now place orders on our shop: <a href="{shop_url}" style="color:#337ff1">{shop_name}</a></span>
        </font>
    </td>
</tr>
 
                        <tr>
                            <td class="space_footer" style="padding:0!important">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="footer" style="border-top:4px solid #333333;padding:7px 0">
                                <span><a href="{shop_url}" style="color:#337ff1">{shop_name}</a> powered by <a href="http://www.prestashop.com/" style="color:#337ff1">PrestaShop&trade;</a></span>
                            </td>
                        </tr>
                    </table>
                </td>
                <td class="space" style="width:20px;padding:7px 0">&nbsp;</td>
            </tr>

Bạn đã hoàn tất! bây giờ có thể test thử rùi.

Sửa lỗi

Khi làm việc với email và Prestashop, bạn sẽ có thể đối mặt với lỗi gửi mail thất bại. Đừng mất hi vọng nếu nó không làm việc ở lần sử dụng đầu tiên và nên kiểm tra lại các vấn đề sau:
– Test tính năng gửi mail từ advanced parameters -> emails. Nhập email người nhận và kiểm tra xem có hoạt động không.
– Đảm bảo nó không rơi vào spam folder trong email.
– Kiểm tra lại tên template mới, đó phải là “account_for_admin”.
– Hãy chắc chắn thêm template vào đúng thư mục mã ngôn ngữ, bạn đang sử dụng. Để chắc chắn bạn thử nhân bản vào tất cả các folder ngôn ngữ.

Nếu bạn vẫn không nhận được email, bạn thử chạy riêng code gửi email bởi hàm Mail::Send, bằng cách sử dụng đoạn code sau:

Mail::Send(
            Configuration::get('PS_LANG_DEFAULT'),
            'account_for_admin',
            Mail::l('New client!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email
                ),
            Configuration::get('PS_SHOP_EMAIL'),
            Configuration::get('PS_SHOP_NAME'),
            null,null,null,null,_PS_MAIL_DIR_,true
        );

Tham số cuối mình thêm vào để nói với hàm sẽ dừng lại nếu gặp lỗi. Nếu nó sẩy ra, bạn sẽ thấy nguyên nhân lỗi tại sao lại không gửi được email.

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Prestashop Tìm kiếm: email, email notifications

Comments

  1. aaaa says

    Thứ Hai, 11/03/2019 at 3:01 chiều

    người đăng ký thành người bán hàng sao shop

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.