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

Sabareesh
  • 358
  • 5
  • 28

1 Answers1

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

PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80