1

I'm trying to display a registration pop-up only if a user has never seen it before and it occurred to me it would be wise to also check to make sure they were not already logged in.

But when I follow the suggestions I have seen to use Magento_Customer/js/model/customer, customer ends up being undefined so I can't check customer.isLogged();

Am I missing something?

define([
    "jquery",
    "Magento_Customer/js/model/customer",
    "mage/cookies"
], function ($, customer) {
    "use strict";
    return function myscript() {
        // it was failing setting the variable so I added this
        console.log('in myscript', customer);
        // the console shows that customer is undefined
        var isLoggedIn = customer.isLoggedIn();
        var regMsgSeen = $.mage.cookies.get('regMsgSeen');
        if(!regMsgSeen && !isLoggedIn) {
            $(document).ready(
                function () {
                    $.mage.cookies.set('regMsgSeen',1,{});
                    setTimeout(
                        function () {
                            $('div#zoo-register-form').modal('openModal');
                        }, 1000);
                });
        }
    }
});

1 Answer 1

0

There are 2 reasons of this case:

  1. customer.isLoggedIn() method in Magento_Customer/js/model/customer check if global var window.isCustomerLoggedIn is true. For first look this var is defined only in Checkout + Cart. So you will get undefined for the rest pages.
  2. I know that Magento makes AJAX request for update sectionDate when user enter to page 1-st time, do login or clear local cache. In this response there is "customerData" object. You have to check if this request is related with customer.isLoggedIn() method (for first look -> no, but maybe it will be tip for you)
1
  • I think it's not loaded yet, so it probably is some kind of ajax thing. I saw one example that uses the jquery deferred, but I'm not familiar with how that process works, no less in conjunction with magento no less in conjunction with another script.
    – Scott
    Commented Jul 12, 2018 at 20:44

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.