1

I want to achieve below functionality.

When user visit my website I want to setup cookie for example 30 second, After 30 second if user is not register then ask him/her for registration. It is just like registration reminder.

Can anyone help me how can I Implement this functionality.

Any help would be appreciate.

2 Answers 2

2

You can check customer is logged in or not using below code in your custom module js

require(['jquery', 'Magento_Customer/js/model/customer'],function($, customer){
    jQuery(document).ready(function(){
        var timer = setInterval(function(){        
            if(customer.isLoggedIn()){
                //Customer logged in
            } else {
                //Customer not logged in
            }
            clearTimeout(timer);
        },30000);
    });
});
3
  • 1
    Let me check... Commented Jun 25, 2019 at 7:01
  • @Sanjay Gohil,you can not request any person to upvote..If a user wants then they can do upvote themself
    – Amit Bera
    Commented Jun 25, 2019 at 7:18
  • 1
    I will keep this thing in my mind, next time this will not happens. Thanks for your advise..! @AmitBera Commented Jun 25, 2019 at 7:24
2

Use below code for check user login or no

protected $_session;
protected $_context;
protected $objectManager;

public function __construct(
    \Magento\Customer\Model\Session $session,
    \Magento\Framework\View\Element\Template\Context $context
) {     
    parent::__construct($context);
    $this->objectManager = $context->getObjectManager();
} 


public function isCustomerLoggedIn()
{
    $session = $this->objectManager->get('Magento\Customer\Model\Session');    
    if($session->isLoggedIn()) {
       $status = "Customer logged in";
    }else{
        $status = "Not customer logged in";
    }
    return $status;
}

Also you can use check user login or not using cookies

define(['Magento_Customer/js/model/customer'], 
    function(customer) {
        return Component.extend({
            someMethod: function () {
                if (customer.isLoggedIn()) {
                   //do smth
                } else {
                   //do smth
                }
            },
        });
    }
);

Get Reference

2
  • 1
    What about cookie? Commented Jun 25, 2019 at 6:44
  • Code is updated . plz review code
    – Shorabh
    Commented Jun 25, 2019 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.