Như các bạn đã biết, có nhiều trường hợp chúng ta cần phải kiểm tra xem liệu customer đã đăng nhập hay chưa trên website của bạn. Ví dụ website có một số tính năng chỉ dành cho các customer đã đăng nhập. Vì thế bạn cần phải kiểm tra xem customer đã đăng nhập hay chưa để hiển thị các tính năng đó ra. Trong bài viết này tôi sẽ hướng dẫn bạn cách để kiểm tra customer đã đăng nhập hay chưa.
Có vài cách để bạn có thể kiểm tra customer đã đăng nhập hay chưa, thông qua PHP và JS
1. Kiểm tra customer đã đăng nhập hay chưa thông qua class Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace Vendor\Extension\Controller\Customer; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Customer\Model\Session; <em>/** * Class Index * </em><strong><em>@package </em></strong><em>Vendor\Extension\Controller\Customer */ </em>class Index extends Action { <em>/** * </em><strong><em>@var </em></strong><em>Session */ </em>private $customerSession; <em>/** * Index constructor. * </em><strong><em>@param </em></strong><em>Context $context * </em><strong><em>@param </em></strong><em>Session $session */ </em>public function __construct( Context $context, Session $session ) { $this->customerSession = $session; parent::__construct($context); } public function execute() { // by using Session model if ($this->customerSession->isLoggedIn()) { // customer login code } else { // customer not login } } } |
2. Kiểm tra customer đã đăng nhập hay chưa thông qua HTTP context
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace Vendor\Extension\Controller\Customer; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Http\Context as AuthContext; <em>/** * Class Index * </em><strong><em>@package </em></strong><em>Vendor\Extension\Controller\Customer */ </em>class Index extends Action { <em>/** * </em><strong><em>@var </em></strong><em>AuthContext */ </em>private $authContext; <em>/** * Index constructor. * </em><strong><em>@param </em></strong><em>Context $context * </em><strong><em>@param </em></strong><em>AuthContext $authContext */ </em>public function __construct( Context $context, <em>AuthContext </em>$authContext ) { $this->authContext = $authContext; parent::<em>__construct</em>($context); } public function execute() { // using HTTP context $isLoggedIn = $this->authContext->getValue(\Magento\Customer\Model\Context::<em>CONTEXT_AUTH</em>); if ($isLoggedIn) { //your coding } } } |
3. Kiểm tra customer đã đăng nhập hay chưa thông qua JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<strong><em>define</em></strong>([ 'uiComponent', 'Magento_Customer/js/model/customer' ], function ( Component, customer ) { 'use strict'; return Component.extend({ <em>/** * Check if customer is logged in * * </em><strong><em>@return </em></strong><em>{boolean} */ </em>isLoggedIn: function () { return customer.isLoggedIn(); } }); }); |
Lưu ý rằng cách này chỉ khả dụng cho Magento 2.3 trở lên.
Bài viết liên quan
Tham khảo: https://meetanshi.com/blog/check-if-a-customer-is-logged-into-magento-2/