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!
Solved! Go to Solution.
@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)
Thanks!
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.
@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)
Thanks!