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>
14
  • please use require instead of define in phtml file: <script type="text/javascript"> require(['Magento_Customer/js/model/customer'], function (customer) { if (customer.isLoggedIn()) { // Do your Logic. } }); </script> – bhargav shastri Apr 8 at 5:10
  • @bhargavshastri please check i have tried both solution and already shared the code. – Vikas kalal Apr 8 at 5:12
  • but you have used wrong code in second solution whereas in first code just use require, it should work – bhargav shastri Apr 8 at 5: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 8 at 5:17
  • i have tried below code in my magento2 instance phtml file and its working fine in every pages: <script type="text/javascript"> require(['Magento_Customer/js/model/customer'], function (customer) { if (customer.isLoggedIn()) { alert('logged-in'); } else { alert('not logged-in'); } }); </script> – bhargav shastri Apr 8 at 5:20
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>
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.

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.