从 Woocommerce 中的 URL 变量填写结帐字段值

Aar*_*des 2 php wordpress get checkout woocommerce

我有一个包含很多变量的 URL,使用 GET 例如 checkout/?FirstName=Test&LastName=Test&EmailAddress=Test

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

任何帮助谢谢

Loi*_*tec 5

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

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

The code:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明