使用会话中保存的 Url 变量预填充 Woocommerce 结账字段

Pre-fill Woocommerce checkout fields with Url variables saved in session(使用会话中保存的 Url 变量预填充 Woocommerce 结账字段)
本文介绍了使用会话中保存的 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 文件中.经过测试并有效.

这篇关于使用会话中保存的 Url 变量预填充 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 的问题)