cancel
Showing results for 
Search instead for 
Did you mean: 

Run JS in a phtml

SOLVED

Run JS in a phtml

Hi, i have my .phtml file and i need run my JS, just when the customers is logged.

My JS.

<script type="text/javascript">
    require(['jquery', 'jquery/ui'], function($){
      jQuery(document).ready( function() {
        jQuery("#redirectButton").click(function(event){
                var link = '<?php echo $block->getUrl('customer/account/login') ?>';
                if(link){
                    event.preventDefault();
                    window.location.href = link;
                }
        });
      });
    });
</script>

How can i check if the customer is logged? is possible with another js in the same phtml? is this possible? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Run JS in a phtml

@ignacio_m you can check whether the customer is logged in or not through php code or through js code also.

 

Please refer the below links and let me know if you stuck anywhere.

 

https://www.rakeshjesadiya.com/customer-is-logged-in-using-knockout-js-magento-2/  (Js code)

 

https://magento.stackexchange.com/questions/91897/how-to-check-if-customer-is-logged-in-or-not-in-ma... (php code)

 

Thanks!

View solution in original post

2 REPLIES 2

Re: Run JS in a phtml

Hello @ignacio_m 

 

Do you want to run the above JS when customers click on “Sign In” or do you have any other purpose?
If you want to redirect customers to the login page on clicking any element, then we suggest doing that with HTML “a” tag instead of with JS.

 

Thanks.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: Run JS in a phtml

@ignacio_m you can check whether the customer is logged in or not through php code or through js code also.

 

Please refer the below links and let me know if you stuck anywhere.

 

https://www.rakeshjesadiya.com/customer-is-logged-in-using-knockout-js-magento-2/  (Js code)

 

https://magento.stackexchange.com/questions/91897/how-to-check-if-customer-is-logged-in-or-not-in-ma... (php code)

 

Thanks!