1

I have found the below code. but it's not working and showing define error in frontend if i use this code in phtml file. Like this error shown : require.js:166 Uncaught Error: Mismatched anonymous define() module: function($,customerData){

<script type="text/javascript">
    define(['Magento_Customer/js/model/customer'], function (customer) {
        if (customer.isLoggedIn()) {
            // Do your Logic.
        }
    });
</script>

Also i have tried the below code in phtml file to get customer data in js but it always return null or ko observer object.

<script type="text/javascript">
    require([ 'jquery', 'Magento_Customer/js/customer-data'],function($,customerData){
        var customer = customerData.get('customer')();
        console.log(customerData.fullname);
        if (customer.fullname && customer.firstname)
        {
            alert(customer.fullname);
        }else{
            alert(customer.fullname);
        } 
    });
</script>
Vikas kalal
  • 1,251
  • 1
  • 6
  • 25
  • please use require instead of define in phtml file: – bhargav shastri Apr 08 '21 at 05:10
  • @bhargavshastri please check i have tried both solution and already shared the code. – Vikas kalal Apr 08 '21 at 05:12
  • but you have used wrong code in second solution whereas in first code just use require, it should work – bhargav shastri Apr 08 '21 at 05:15
  • 'Magento_Customer/js/model/customer' this is used to get data on checkout only for rest of page i need to use other js which i mentioned in require function. – Vikas kalal Apr 08 '21 at 05:17
  • i have tried below code in my magento2 instance phtml file and its working fine in every pages: – bhargav shastri Apr 08 '21 at 05:20
  • i have tried your code and it always return else part on rest of page and on cart page it return true.Please try in your local you will get the result. – Vikas kalal Apr 08 '21 at 05:29
  • Please check the screenshot for clarity : Result : https://i.imgur.com/n53g13s.png Code : https://i.imgur.com/Qy2blFI.png – Vikas kalal Apr 08 '21 at 05:43
  • @Vikaskalal, Every .phtml file will load the data from Block file. Why do you using extra code to validate user in .js file instead of check with user session in Block file? – Bojjaiah Apr 08 '21 at 05:56
  • yes i tried the php code but with that we get fpc cache issue.not get real current data @Bojjaiah – Vikas kalal Apr 08 '21 at 06:01
  • @Vikaskalal Have you checked with cacheable="false" in layout file? – Bojjaiah Apr 08 '21 at 06:06
  • @Bojjaiah after this it work well but not recommended to use right now.any other way is appricetable. instead of it. – Vikas kalal Apr 08 '21 at 06:15
  • @Vikaskalal look on it https://magento.stackexchange.com/questions/157227/magento-2-get-customer-lastname-in-header-phtml – Bojjaiah Apr 08 '21 at 06:36
  • i want to check in js code is any example i have tried every code in js but not get succed @Bojjaiah your help will be appricatable. – Vikas kalal Apr 08 '21 at 07:04

2 Answers2

1

I have searched so many times and tried lot of things to check whether customer is logged in or not in phtml file. in js script but not found any solution. i have achieved it myself by doing some tricky things. as it's a temporary solution. if someone as better answer for this query then it help would be appreciable. And This code will work on every pages.

<script type="text/javascript">
    require([ 'jquery'], function($){
        jQuery(document).ready(function(){
            setTimeout(function () {
                var customerData = JSON.parse(localStorage['mage-cache-storage']);
                if (customerData.customer) {
                    var customer = customerData.customer;
                    if (customer.fullname && customer.firstname) {
                        console.log('Logged in');
                    } else {
                        console.log('Not logged in');
                    }
                }
            }, 5000);
        });
    });
</script>
Vikas kalal
  • 1,251
  • 1
  • 6
  • 25
0

In your first example you're using define() inline, rather than require(), which is causing the error. You can confirm this yourself using your browsers' web console, by copying and pasting the script and running it.

Try running this: (note that I changed the define to require)

    require(['Magento_Customer/js/model/customer'], function (customer) {
        if (customer.isLoggedIn()) {
            console.log('Logged in');
        }
        else {
            console.log('Not logged in');
        }
    });

inside your browser web console, you will get a console log depending on whether or not you're logged in.

When using RequireJS you use define() when you are creating a new module, and you use require() when you just need to load a dependency. In this case, you're not creating a new module but just trying to load the Magento_Customer/js/model/customer dependency, so you should be using require() here.

Adam Mellen
  • 339
  • 1
  • 11