0

I want find the whether the customer is logged in or not using js. I got some links but it doesn't works and i not good at knockout js. Please give solution with brief explanation.

I can't get the correct answer for this and the solutions I got didn't work to me please help to find the solution.

Refered Link:Magento 2.3 How to check that customer is logged in using Js

1 Answer 1

1

We will include a js file in theme :

Supposing that your js file is: myfile.js

app/design/frontend/{Vendor}/{theme}/requirejs-config.js

var config = {
    map: {
        '*': {
            biboo: 'js/myfile'
        }
    }
};

app/design/frontend/{Vendor}/{theme}/web/js/myfile.js

define(['jquery', 'Magento_Customer/js/model/customer'], function($, customer){
   "use strict";
       return function ifCustomerIsLoggedIn()
       {
           if (customer.isLoggedIn()) {
               console.info('Yes, customer is logged in');
               //your logic goes here
           } else {
               console.info('No, customer is not logged in');
               //your logic goes here
           }
       }
});

app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/{yourfile}.phtml

<script>
    require(['jquery', 'biboo'], function($, ifCustomerIsLoggedIn) {
        ifCustomerIsLoggedIn();
    });
</script>

 

clean the cache

clean var/view_preprocessed content

clean pub/static content

deploy the static content = php bin/magento setup:static-content:deploy -f

Output :

enter image description here

1
  • Thank you @prince i will check and give you the update
    – Sabareesh
    Commented Feb 28, 2020 at 10:39

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.