Search code examples
phpwordpresswoocommercegetcheckout

Fill checkout fields values from URL variables in Woocommerce


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

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?

Any help thanks


Solution

  • 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;
    }
    

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

    enter image description here