dongzhi2887 2019-02-07 23:38
浏览 52
已采纳

使用保存在会话中的URL变量预填充Woocommerce登录字段

I am using "Pre-fill Woocommerce checkout fields with Url variables saved in session" answer code, trying to populate Woocommerce login username and password with variables saved in session.

This is my customized code in functions.php so far:

// Save user data from URL to Woocommerce session, this works fine
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
    if ( isset( $_GET['sliced_client_email'] ) || isset( $_GET['tu_name'] ) ) {
        $email   = isset( $_GET['sliced_client_email'] )   ? esc_attr( $_GET['sliced_client_email'] )   : '';
        $pw = isset( $_GET['password'] ) ? esc_attr( $_GET['password'] ) : '';

        // Set the session data
        WC()->session->set( 'custom_data', array( 'email' => $email, 'password' => $pw ) );
    }
}

// Autofill checkout fields from user data saved in Woocommerce session, this is my problem
add_filter( 'woocommerce_login_form' , 'prefill_login_fields' );
function prefill_login_fields ( $xxx ) {
    // Get the session data
    $data = WC()->session->get('custom_data');

    // Email
    if( isset($data['email']) && ! empty($data['email']) )
       $xxx['username']['default'] = $data['email'];

    // Password
    if( isset($data['password']) && ! empty($data['password']) )
        $xxx['password']['default'] = $data['password'];
}

But of course I can't find the parameters to populate. On the checkout page there is $address_fields['billing_email'] but I can't find a similar parameter for the login page. Not sure what to put in $xxx and $xxx['username']['default'] and $xxx['password']['default'].

Thanks for the help!!

  • 写回答

1条回答 默认 最新

  • duanhanglekr37902 2019-02-08 01:26
    关注

    Alright so instead of using variables stored in session, I found a simple jQuery solution.

    add_action('woocommerce_login_form','woocommerce_js_2');
    
    function woocommerce_js_2()
    { // break out of php 
    ?>
    <script>
    // Setup a document ready to run on initial load
    jQuery(document).ready(function($) {
        var r = /[?|&](\w+)=(\w+)+/g;  //matches against a kv pair a=b
        var query = r.exec(window.location.href);  //gets the first query from the url
        while (query != null) {
    
                //index 0=whole match, index 1=first group(key) index 2=second group(value)
            $("input[name="+ query[1] +"]").attr("value",query[2]);
    
            query = r.exec(window.location.href);  //repeats to get next capture
        }
    
    });
    </script>
    <?php } // break back into php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里