基于购物车的 WooCommerce 登录重定向

WooCommerce login redirect based on cart(基于购物车的 WooCommerce 登录重定向)
本文介绍了基于购物车的 WooCommerce 登录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想申请以下两种情况:

I want to apply following 2 case :

  • 如果用户未登录且购物车为空:然后重定向用户登录,然后重定向我的帐户
  • 如果用户未登录并且购物车有产品:然后重定向用户登录,登录后重定向到结帐

我的代码:

 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 ) {} 检查购物车时,我的网站停止在职的.

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.

我已将此代码添加到我的主题的functions.php 文件中.

I have added this code in my theme's functions.php file.

我做错了什么?

推荐答案

为避免您的网站关闭, global $woocommerce; 缺失.
现在带有 $woocommerce->cartglobal $woocommerce; 现在简单地替换为 WC()->cart.

To avoid your site to be off, global $woocommerce; is missing.
Now global $woocommerce; with $woocommerce->cart is now simply replaced by WC()->cart.

要检查购物车是否为空,您应该使用 WC()->cart->is_empty(),如 is_empty()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.

之后,在结帐页面(在两种情况下)如果用户未登录,您希望将他重定向到 my_account 页面(登录/创建帐户区域).

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).

现在在 my_account 页面,当登录用户的购物车中有东西时,您希望将他重定向到结帐页面.

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 文档):

  • Woocommerce Class WC_Cartis_empty() 方法
  • WooCommerce 可用的条件标签
  • Woocommerce Class WC_Cartis_empty() method
  • WooCommerce Available conditional tags

这篇关于基于购物车的 WooCommerce 登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)