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.
-
please try : gist.github.com/pawankparmar/621425df47fee5815251b8e6046487f1Pawan– Pawan2019年02月28日 18:02:15 +00:00Commented Feb 28, 2019 at 18:02
-
thanks, not solution, because i use page_cache :) the issue still actualDenissio Inkognito– Denissio Inkognito2019年02月28日 18:19:10 +00:00Commented 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?Typo– Typo2020年02月10日 20:33:31 +00:00Commented Feb 10, 2020 at 20:33
2 Answers 2
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.
-
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:1Denissio Inkognito– Denissio Inkognito2019年02月28日 19:00:29 +00:00Commented Feb 28, 2019 at 19:00
-
and even from customer.isLoggedIn I cannot get the customer statusDenissio Inkognito– Denissio Inkognito2019年02月28日 19:02:49 +00:00Commented Feb 28, 2019 at 19:02
-
i109.fastpic.ru/big/2019/0228/66/…Denissio Inkognito– Denissio Inkognito2019年02月28日 19:20:24 +00:00Commented 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.obervableonwindow.isCustomerLoggedIn, so your approach to get the data usingcustomer.isLoggedIn()is correct. I've updated my answer with what I found out.HelgeB– HelgeB2019年02月28日 19:29:21 +00:00Commented Feb 28, 2019 at 19:29
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);
Explore related questions
See similar questions with these tags.