微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Woocommerce 的 get_checkout_payment_url 上创建自动登录链接

如何解决在 Woocommerce 的 get_checkout_payment_url 上创建自动登录链接

我正在查看本指南 https://www.pirenko.com/creating-wordpress-auto-login-links/ 并尝试将其调整为使用 customer-invoice.PHP 模板的报价邮件

问题:

  1. 如何获取订单的正确密钥?
  2. 如何使用密钥更新当前用户,以便我可以为 URL 调用它?
$secret_string=wp_generate_password( 8,false );
$login_link ='http://website.com/wp-json/sand_login/prk_route?username='.$username.'&secret='.$user_info->secret_key;

我首先删除了对密钥的检查,只是想看看我是否可以让它与电子邮件地址一起工作,并添加一个重定向到结帐。一旦我可以在当前用户元数据上更新安全密钥,我将添加对安全密钥的检查。

添加到functions.PHP

add_action( 'rest_api_init',function () {
    register_rest_route(
        'sand_login','prk_route',array(
            'methods' => 'GET','callback' => 'pirenko_login_user',)
    );
});

function pirenko_login_user() {

    // Validate input
    if (isset($_GET['username']) && $_GET['username']!="") {

        $username = $_GET['username'];
        $user = get_user_by('email',$username );

        if ($user) {

            //Check if secret key matches
            $check_user = get_user_by( 'id',$user->ID );

                //Login info is correct - let's proceed

                // Manage login cookies
                wp_clear_auth_cookie();
                wp_set_current_user ( $user->ID );
                wp_set_auth_cookie ( $user->ID );

                // Finally redirect user
                $order = new WC_Order($order_id);               
                $payment_page = $order->get_checkout_payment_url($order->ID);
                wp_safe_redirect( $payment_page );
                exit();
            

        }
        else {
            echo "Could not find user!";
            exit();
        }
    }
    else {
        echo "Parameters are missing!";
        exit();
    }
}

customer-invoice.PHP 中的电子邮件函数如下所示:

if ($order && $order->has_status('enquiry')) {
    $email_text = str_replace('{betaallink}','<a class="payment" href="' . esc_url($order->get_checkout_payment_url()) . '">',$email_text);
    $email_text = str_replace('{/betaallink}','</a>',$email_text);
}

我更新到这个,没有开始的秘钥:

if ($order && $order->has_status('enquiry')) {
    $user = $order->get_user();
    $username = $user ? $user->__get('user_email') : '';
    $login_link ='http://website.com/wp-json/sand_login/prk_route?username='.$username;
    $email_text = str_replace('{betaallink}','<a class="payment" href="'. $login_link .'">',$email_text);
}

这有效。用户登录并使用此 URL 重定向到购物车,其中包含用户注册的所有未结订单:

https://website.com/afrekenen/order-pay/?pay_for_order=true&key

我这样做是完全错误的吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。