填写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.