1

I have a html file not a .phtml file, i want to check custom is log in or not in html file, how can i do this?

3
  • An html file is not the place to check whether the customer is logged in or not Commented Jun 28, 2019 at 8:07
  • I want to hide one button in checkout page but the button code are store in html page, how can i hide that button if customer is not login @vitoriodachef Commented Jun 28, 2019 at 8:41
  • Refer the link for same magento.stackexchange.com/questions/203951/… Commented Jun 28, 2019 at 8:50

1 Answer 1

0

You have to create a function in knockout and call this function to html file.

You need to pass dependency of customer object, 'Magento_Customer/js/model/customer'

Your JS should be like this.

define(
[
    'ko',
    'jquery',   
    'Magento_Customer/js/model/customer',
    'mage/translate'
],
function (ko,jquery, customer,$t) {
    'use strict';
    return Component.extend({        
        isCustomerLoggedIn: customer.isLoggedIn, /*return  boolean true/false */
        initialize: function() {
            this._super();
            /* check using below method */
            var isLoggedIn = this.isCustomerLoggedIn();
        }
    });
}

And in your phtml file call function data-bind="attr: { attr: isCustomerLoggedIn()}"

Take reference from magento2 check if customer is logged in or not in knockout template

I hope it helps!

3
  • It gives me error isCustomerLoggedIn is not a function Commented Jun 28, 2019 at 11:14
  • I have just shared logic customer.isLoggedIn using this you can get customer logged in or not, you have to create function. Commented Jun 28, 2019 at 11:21
  • okay thank you. Commented Jun 28, 2019 at 13:00

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.