[英]How to redirect logged out users to my account page instead of checkout page
I am trying to redirect logged out users to my account page when they try to checkout.当他们尝试结帐时,我试图将注销的用户重定向到我的帐户页面。
I have tried this but its now working我试过这个,但它现在工作
function wpse_131562_redirect() {
if (
! is_user_logged_in()
&& (is_cart() || is_checkout())
) {
wp_redirect('woocommerce_myaccount_page_id'());
exit;
}
}
add_action('template_redirect', 'wpse_131562_redirect');
Your code can't work as there is no redirect link… Try the following instead (redirecting unlogged user from Checkout to My Account) :您的代码无法工作,因为没有重定向链接……请尝试以下操作(将未登录的用户从 Checkout 重定向到我的帐户) :
add_action('template_redirect', 'unlogged_my_account_redirect');
function unlogged_my_account_redirect() {
if ( ! is_user_logged_in() && is_checkout() && ! is_wc_endpoint_url() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.