dongqing6661 2017-01-17 14:10
浏览 134

WooCommerce:如果没有登录,则重定向到我的帐户并返回结帐

I'm trying to create a custom registration link on the checkout page. Here's the snippet I used:

    add_action('woocommerce_checkout_fields', 'add_registration_custom_link');
    function add_registration_custom_link(){
    if(is_checkout()){
    if (!is_user_logged_in()){
    $info_message = apply_filters( 'woocommerce_checkout_login_message', __( 'New customer?', 'woocommerce' ) );
   $info_message .= ‘<a href="http://www.mysite.co.uk?redirect_to=http://www.mysite.co.uk/checkout">’ . __( ‘Click here to Register’, ‘woocommerce’ ) . ‘</a>’;
    wc_print_notice( $info_message, 'notice' );
    }
    }
    }

The idea is to print a message + link that prompts users to register/log in if not already registered/logged in. It will replace the default login form at the top of the checkout page. I want to redirect users to the My Account page when they click on the "Click here to register" link so they can log in/register, and then redirect them back to checkout once successful.

So far, the code above isn't working and is generating additional issues on the page (i.e. invalid arguments in the shipping and billing forms). Any ideas how to get this to work properly?

UPDATE: I found this handy snippet and added it to my functions.php file:

function woocommerce_custom_redirects() {

    // 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') ) );


}
add_action('template_redirect', 'woocommerce_custom_redirects');

This redirects the user to the My Account page if not logged in. I need an extra argument to redirect them back to the checkout page once logged in/registered. Any ideas?

Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波
    • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
    • ¥15 钢筋实图交点识别,机器视觉代码
    • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
    • ¥50 400g qsfp 光模块iphy方案
    • ¥15 两块ADC0804用proteus仿真时,出现异常