基于购物车的 WooCommerce 登录重定向
WooCommerce login redirect based on cart
我想申请以下2个案例:
- 如果用户未登录且购物车为空:然后将用户重定向到登录,然后是我的帐户
- 如果用户未登录且购物车中有产品:然后将用户重定向到登录并在登录后重定向到结帐
我的代码:
function wpse_Nologin_redirect() {
if (
! is_user_logged_in()
&& (is_checkout())
) {
// feel free to customize the following line to suit your needs
$MyLoginURL = "http://example.in/my-account/";
wp_redirect($MyLoginURL);
exit;
}
}
add_action('template_redirect', 'wpse_Nologin_redirect');
以上代码适用于我的第一个案例。但是对于我的第二种情况,当我使用 if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {}
检查购物车时,我的网站停止工作。
我已将此代码添加到我主题的 functions.php 文件中。
我做错了什么?
To avoid your site to be off, global $woocommerce;
is missing.
Now global $woocommerce;
with $woocommerce->cart
is now simply replaced by WC()->cart
.
To check if cart is empty, you should use WC()->cart->is_empty()
, as is_empty()
is a conditional method of WC_cart
class.
After, on checkout page (in both cases) if user is not logged in, you want to redirect him to my_account page (login/create account area).
Now on my_account page, when a logged user has something in his cart, you want to redirect him on checkout page.
这是您需要的代码:
add_action('template_redirect', 'woocommerce_custom_redirections');
function woocommerce_custom_redirections() {
// Case1: Non logged user on checkout page (cart empty or not empty)
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// Case2: Logged user on my account page with something in cart
if( is_user_logged_in() && ! WC()->cart->is_empty() && is_account_page() )
wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
}
代码进入您的活动子主题的 function.php 文件。 已测试并有效。
参考资料(Woocommerce 文档):
我想申请以下2个案例:
- 如果用户未登录且购物车为空:然后将用户重定向到登录,然后是我的帐户
- 如果用户未登录且购物车中有产品:然后将用户重定向到登录并在登录后重定向到结帐
我的代码:
function wpse_Nologin_redirect() {
if (
! is_user_logged_in()
&& (is_checkout())
) {
// feel free to customize the following line to suit your needs
$MyLoginURL = "http://example.in/my-account/";
wp_redirect($MyLoginURL);
exit;
}
}
add_action('template_redirect', 'wpse_Nologin_redirect');
以上代码适用于我的第一个案例。但是对于我的第二种情况,当我使用 if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {}
检查购物车时,我的网站停止工作。
我已将此代码添加到我主题的 functions.php 文件中。
我做错了什么?
To avoid your site to be off,
global $woocommerce;
is missing.
Nowglobal $woocommerce;
with$woocommerce->cart
is now simply replaced byWC()->cart
.To check if cart is empty, you should use
WC()->cart->is_empty()
, asis_empty()
is a conditional method ofWC_cart
class.After, on checkout page (in both cases) if user is not logged in, you want to redirect him to my_account page (login/create account area).
Now on my_account page, when a logged user has something in his cart, you want to redirect him on checkout page.
这是您需要的代码:
add_action('template_redirect', 'woocommerce_custom_redirections');
function woocommerce_custom_redirections() {
// Case1: Non logged user on checkout page (cart empty or not empty)
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// Case2: Logged user on my account page with something in cart
if( is_user_logged_in() && ! WC()->cart->is_empty() && is_account_page() )
wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
}
代码进入您的活动子主题的 function.php 文件。 已测试并有效。
参考资料(Woocommerce 文档):