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

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);
    });
});
  • 1
    Let me check... – Sanjay Gohil Jun 25 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 Jun 25 at 7:18
  • 1
    I will keep this thing in my mind, next time this will not happens. Thanks for your advise..! @AmitBera – Sanjay Gohil Jun 25 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

  • 1
    What about cookie? – Sanjay Gohil Jun 25 at 6:44
  • Code is updated . plz review code – Shorabh Jun 25 at 6:51
  • 1
    +1 for great idea – Sanjay Gohil Jun 25 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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