4

In my project I'm using a direct checkout plugin for woocommerce in order to have cart and checkout on one page.

And the client want's the order process to be like this

  1. We have an order page in wordpress where users enters their name, address, phone, email, selects the price and then redirects to a direct checkout.
  2. On checkout page we want fields billing_name, billing_address, etc to be already prefilled with a data from order page.

So the question is how could I pass that data to a checkout ?

And the other how could I clear those fields before filling with a passed data ?

Oleg
  • 41
  • 1
  • 1
  • 3

2 Answers2

13

This solution helped me - https://www.bobz.co/pre-populate-woocommerce-checkout-fields/

<?php
/**
 * Pre-populate Woocommerce checkout fields
 */
add_filter('woocommerce_checkout_get_value', function($input, $key ) {
    global $current_user;
    switch ($key) :
        case 'billing_first_name':
        case 'shipping_first_name':
            return $current_user->first_name;
        break;

        case 'billing_last_name':
        case 'shipping_last_name':
            return $current_user->last_name;
        break;
        case 'billing_email':
            return $current_user->user_email;
        break;
        case 'billing_phone':
            return $current_user->phone;
        break;
    endswitch;
}, 10, 2);
gorodezkiy
  • 3,299
  • 2
  • 34
  • 42
6

I just came across this same issue on a site I'm building. We're collecting email addresses in a survey and if the user goes on to checkout, we wanted the email field to be pre-filled. I'm storing the email address in the WooCommerce session when the survey form is submitted like this:

$woocommerce->session->set('survey_email', $_POST['survey_email']);

Then, I'm hooking into the woocommerce_checkout_fields filter (see docs) to fill the form field. You can't directly set the value of the field, but you can set what the default value is, which then has the benefit of being overwritten if the user logs in.

function override_checkout_email_field( $fields ) {
    global $woocommerce;
    $survey_email = $woocommerce->session->get('survey_email');
    if(!is_null($survey_email)) {
      $fields['billing']['billing_email']['default'] = $survey_email;
    }
    return $fields;
}

add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );
tyler_paulson
  • 61
  • 1
  • 4