0

Hi I've been scratching my head trying to figure out why at least one of these scripts doesn't work for me. I've tried them being logged out and still getting nothing. Any help figuring out what is wrong would be appreciated.

I'm trying to pass this string to Woocommerce one-page checkout page. I'm not using cartflows.

I've created a document with all of the steps to take in my ultimate application. It also includes all of the snippets I've gleaned and attempted.

https://docs.google.com/document/d/153elfEFpoAKOwE_PLErz4B4WM9qMX6b_G7Eeja4m-n8/edit?usp=sharing

Snippet 1 & 2 do nothing. Snippet 4 produces a critical error.

Here's my string https://sizzlinghotmarriage.com/dont-give-up-on-love-checkout/?add-to-cart=24631&quantity=1,fname=[1886 sanitize_url=1],lname=[1887 sanitize_url=1],email=[1820 sanitize_url=1],phone=[1821 sanitize_url=1],address1=[1822 sanitize_url=1],address2=[1888 sanitize_url=1],city=[1823 sanitize_url=1],state=[1824 sanitize_url=1],zip=[1825 sanitize_url=1],country=[1826 sanitize_url=1]

Here's snippet 1 /**

// Hook into WooCommerce checkout fields. add_filter( 'woocommerce_checkout_fields' , 'ts_prefill_checkout_fields');

function ts_prefill_checkout_fields ( $checkout_fields ) {

/**
 * Query string fields to populate in checkout,
 *
 * Ex.
 * 'fname' => is query parameter name,
 * 'billing_first_name' => matching field of checkout 
 *
 * You can add other fields in the same way
 */
$query_fields = array(
     
    'fname' => 'billing_first_name',
    'lname' => 'billing_last_name',
    'email' => 'billing_email',
    'phone' => 'billing_phone',
    'address1' => 'billing_address1',
    'address2' => 'billing_address2',
    'city' => 'billing_city',
    'state' => 'billing_state',
    'zip' => 'billing_postcode',
    'country' => 'billing_country',
);

// We will loop the above array to check if the field exists in URL or not.
// If it exists we will add it to the checkout field's default value

foreach ( $query_fields as $field_slug => $match_field ) {
     
    // Check if it is in URL or not, An not empty.
    if ( isset( $_GET[ $field_slug ] ) && ! empty( $_GET[ $field_slug ] ) ) {

        // Sanitize the value and store it in a variable.
        $field_value = sanitize_text_field( $_GET[ $field_slug ] );

        // Check if match field exists in checkout form or not.
        if( isset( $checkout_fields['billing'][ $match_field ] ) ){

            // Assign the pre-fill value to checkout field
            $checkout_fields['billing'][ $match_field ]['default'] = $field_value;
        }
    }       
}


// Return the fields
return $checkout_fields;

}

I've adapted each script to my parameters and each fails to prepopulate woocommerce checkout.

0 Answers0