I am using woocommerce theme shopkeeper and i have problem with proceed to checkout functionality i need a php snippet code for checking the user is login or not when he click a button of proceed to checkout on cart page
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You have to use simple wordpress function to check is a user logged in or not. is_user_logged_in()
Checks if the current visitor is a logged in user. it returns (bool) True if user is logged in, false if not logged in.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
回答2:
First you need to check user in on checkout page or not, if so then do the checking; or if you have your own function then you can simply wordpress is_user_logged_in()
function.
add_action('wp', 'xyz_checkLoggedIn');
function xyz_checkLoggedIn() {
if (is_checkout()) {
if (is_user_logged_in()) {
//user is logged in
} else {
//user is not logged in
}
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.
Hope this helps!
回答3:
add_action('woocommerce_checkout_process', 'check_if_user_loggedin');
function check_if_user_loggedin() {
if ( is_user_logged_in() ) {
echo 'Logged user';
} else {
echo 'Guest user!';
}
}