WooCommerce登录重定向基于购物车 [英] WooCommerce login redirect based on cart
问题描述
我要应用以下2种情况:
- 如果用户未登录且购物车为空:则将用户重定向至登录名,然后我的帐户
- 如果用户未登录并且购物车有商品:然后将用户重定向到登录名,登录后重定向到结帐
我的代码:
函数wpse_Nologin_redirect(){
if(
!is_user_logged_in()
&&(is_checkout())
){
//可以自定义以下行以满足您的需求
$ MyLoginURL = http://example.in/my-account/;
wp_redirect($ MyLoginURL);
出口;
}
}
add_action(‘template_redirect’,‘wpse_Nologin_redirect’);
以上代码在我的第一种情况下工作正常。但是对于第二种情况,当我用 if(sizeof($ woocommerce-> cart-> cart_contents == 0){}
强>,我的网站停止工作。
我已将此代码添加到主题的functions.php文件中。
我做错了什么?
为避免您的网站被关闭,
全球$ woocommerce;
丢失。
现在global $ woocommerce;
与$ woocommerce-> cart
现在简单地替换为WC()->购物车
。
要检查购物车是否为空,您应该使用
WC()-> ; cart-> is_empty()
,如is_empty()
是WC_cart
类 。 p>
之后,在结帐页面(两种情况下)中,如果用户未登录,则要将其重定向到 my_account 页面(登录/创建帐户区域)。
现在在 my_account页面上,当登录用户的购物车中有东西时,您会
这是您需要的代码:
add_action('template_redirect','woocommerce_custom_redirections');
function woocommerce_custom_redirections(){
//情况1:结帐页面上未登录的用户(购物车为空或不为空)
if(!is_user_logged_in()&& is_checkout())
wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')));;
//情况2:在我的帐户页面上用购物车
中的用户登录用户if(is_user_logged_in()&&!WC()->购物车-> is_empty()& ;& is_account_page())
wp_redirect(get_permalink(get_option('woocommerce_checkout_page_id')));;
}
代码会进入活动子主题的function.php文件中。 经过测试并可以正常工作。
参考(Woocommerce文档):
I want to apply following 2 case :
- If User not logged in and cart is empty: Then redirect user to login and then my account
- If User not logged in and cart has product: Then redirect user to login and after login redirect to checkout
My Code :
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');
Above code is working fine for my first case. But for my second case, when I check cart with if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {}
, my site stops working.
I have added this code in my theme's functions.php file.
What I am doing wrong?
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.
Here is the code you need:
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') ) );
}
Code goes in function.php file of your active child theme. Tested and works.
Reference (Woocommerce documentation):
这篇关于WooCommerce登录重定向基于购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!