填写Woocommerce中URL变量的结帐字段值 [英] Fill checkout fields values from URL variables in Woocommerce

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

问题描述

我有一个网址,其中包含很多使用GET进行的变量,例如checkout/?FirstName = Test& LastName = Test& EmailAddress = Test

I have a URL with a lot of variables put in there using GET e.g. checkout/?FirstName=Test&LastName=Test&EmailAddress=Test

我想知道如何使用此信息来预填充WooCommerce结帐页面上的字段?即获取FirstName并在WooCommerce Checkout中填写FirstName?

I was wondering how to prepopulate the fields on the WooCommerce checkout page using this information? i.e. get FirstName and fill out FirstName in WooCommerce Checkout?

任何帮助谢谢

推荐答案

下面是一个示例,该示例基于用于结帐字段的获取url变量来计费名字和姓氏.

Here below is an example with billing first name and last name based on Get url variables for checkout fields.

它基于您的网址示例: checkout/?FirstName = Test& LastName = Test& EmailAddress = Test

Its based on your Url example: checkout/?FirstName=Test&LastName=Test&EmailAddress=Test

代码:

add_filter( 'woocommerce_checkout_get_value' , 'custom_checkout_get_value', 20, 2 );
function custom_checkout_get_value( $value, $imput ) {
    // Billing first name
    if(isset($_GET['FirstName']) && ! empty($_GET['FirstName']) && $imput == 'billing_first_name' )
        $value = esc_attr( $_GET['FirstName'] );

    // Billing last name
    if(isset($_GET['LastName']) && ! empty($_GET['LastName']) && $imput == 'billing_last_name' )
        $value = esc_attr( $_GET['LastName'] );

    // Billing email
    if(isset($_GET['EmailAddress']) && ! empty($_GET['EmailAddress']) && $imput == 'billing_email' )
        $value = sanitize_email( $_GET['EmailAddress'] );

    return $value;
}

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

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

这篇关于填写Woocommerce中URL变量的结帐字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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