使用会话中保存的Url变量预填充Woocommerce结帐字段 [英] Pre-fill Woocommerce checkout fields with Url variables saved in session

查看:126
本文介绍了使用会话中保存的Url变量预填充Woocommerce结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当人们通过销售电子邮件中带有电子邮件和姓名作为参数的链接进入我的woocommerce商店时,我想在结帐页面中预填姓名和电子邮件.

When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout page.

因此,我创建了一个动作和过滤器.这可以按预期工作,但前提是我必须在销售页面(ctrl + f5)上进行硬刷新

Therefore I created an action and filter. This works as expected but only if I do a hard refresh on the sales page (ctrl + f5)

我已从缓存和清漆缓存中排除了销售页面和结帐页面,但这不能解决问题.

I've excluded the sales page and the checkout page from the cache and varnish cache but this didn't fix the issue.

我在这里错过了什么吗?您知道为什么只有硬刷新才能使用它吗?

Am I missing something here? Do you have any idea why this only works with a hard refresh?

非常感谢您的帮助.

代码:

    function save()
    {
    if ( is_page( 'sales-page' ) )
    {
        if ( isset( $_GET['tu_em'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_em', $_GET['tu_em'] );
        }
        if ( isset( $_GET['tu_name'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_name', $_GET['tu_name'] );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'save_email' , 1100);

function override_checkout_email_field( $fields ) {
    global $woocommerce;
    $email = $woocommerce->session->get('tu_em');
    if(!is_null($email)) {
      $fields['billing']['billing_email']['default'] = $email;
    }
    $name = $woocommerce->session->get('tu_name');
    if(!is_null($name)) {
      $fields['billing']['billing_first_name']['default'] = $name;
    }
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );

推荐答案

在下面,您将找到正确的工作代码,将来自URL(GET)的用户数据保存到WooCommerce会话中,并使用相关的结帐自动填充该数据字段.

Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.

URL将类似于:http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName

这可以在任何URL上完成,因为代码在设置后会检测到URL变量.

This can be done from any URL as the code will detect the URL variable, when they are set.

代码;

// Save user data from URL to Woocommerce session
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
    if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
        $em   = isset( $_GET['tu_em'] )   ? esc_attr( $_GET['tu_em'] )   : '';
        $name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';

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

// Autofill checkout fields from user data saved in Woocommerce session
add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
    // Get the session data
    $data = WC()->session->get('custom_data');

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

    // Name
    if( isset($data['name']) && ! empty($data['name']) )
        $address_fields['billing_first_name']['default'] = $data['name'];

    return $address_fields;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于使用会话中保存的Url变量预填充Woocommerce结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆