1

Just wanna to check if customer logged in by javascript use

Magento_Customer/js/model/customer.

I've included following js file in module:

define([
 "jquery",
 'Magento_Customer/js/model/customer',
], function(,ドル customer) {
 'use strict';
 $.widget('conv.bolPrices', {
 _init: function init() {
 console.log('customer', customer);
 console.log('Logged in', customer.isLoggedIn()); //getting undefined
 },
 });
 return $.conv.bolPrices;
});

After login, every time when I am loading page I am getting undefined value, instead true/false.

I wanna to do it use model/customer. I cleared cache, removed generated/*, etc, read a lot of topics about this theme, but had no success.

I very appreciate any help and advice. Thanks.

asked Feb 28, 2019 at 17:39
3
  • please try : gist.github.com/pawankparmar/621425df47fee5815251b8e6046487f1 Commented Feb 28, 2019 at 18:02
  • thanks, not solution, because i use page_cache :) the issue still actual Commented Feb 28, 2019 at 18:19
  • Hi Denissio, could you add some clarification as to how the answer that you checked as correct help you to fix this issue? Commented Feb 10, 2020 at 20:33

2 Answers 2

1

customer.isLoggedIn() is a ko.obervable on window.isCustomerLoggedIn and it seems that window.isCustomerLoggedIn is set by default only in the checkout templates: module-checkout in onepage.phtml and cart/shipping.phtml and module-multishipping in checkout/billing.phtml

So one approach could be to add a similar functionality in some appropriate template and assign Magento\Customer\Model\Session->isLoggedIn() to window.isCustomerLoggedIn in case that window.isCustomerLoggedIn is not defined. Maybe it works in footer template.

One more thought on this issue: If you are using caching (what you most likely do) you should move that to an uncached part if your cache key does not consider if customer is logged in or not.

answered Feb 28, 2019 at 18:39
4
  • customer.isLoggedIn() rerturns undefined, but customer.isLoggedIn returns following: Customer loggedIn ƒ observable() { if (arguments.length > 0) { // Write // Ignore writes if the value hasn't changed if (observable.isDifferent(_latestValue, arguments[0])) {... product-5:1 Commented Feb 28, 2019 at 19:00
  • and even from customer.isLoggedIn I cannot get the customer status Commented Feb 28, 2019 at 19:02
  • i109.fastpic.ru/big/2019/0228/66/… Commented Feb 28, 2019 at 19:20
  • Ok, sorry for the first answer. I've checked the code of the js model and isLoggedIn is a ko.obervable on window.isCustomerLoggedIn, so your approach to get the data using customer.isLoggedIn() is correct. I've updated my answer with what I found out. Commented Feb 28, 2019 at 19:29
0

Takes time to populate customer info into CustomerData.

Try the following code:

var self = this;
var counter = 0;
var timeTopMenu = setInterval(function () {
 self.customer = customerData.get('customer');
 if (self.customer().fullname != undefined) {
 // custom code here
 clearInterval(timeTopMenu);
 } else {
 counter++;
 if(counter > 5) {
 clearInterval(timeTopMenu);
 }
 }
}, 1000);
answered Mar 1, 2019 at 3:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.