Skip Navigation

[Resolved] Pass CRED Fields / First Name to WooCommerce Billing_First_Name

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using CRED Commerce to create new Users. I would like to capture the User's first name, last name, address, and email address in the CRED User form, then pre-populate the checkout fields with that information.

Solution: This will require custom code using the CRED Commerce API cred_commerce_form_action:
Within the callback function, you will have access to anything captured by the form in the $_POST superglobal. You can use that information to construct a checkout URL with custom URL parameters, then trigger a redirect to the custom URL - something like

yoursite.com/checkout?first=John&last=Smith&address=123&email=abc

Then in your filter, you can access those variables from the $_GET superglobal. Here's a very simple example:

add_action('cred_commerce_form_action', 'my_commerce_form_action',10,4);
function my_commerce_form_action( $action, $form_id, $post_id, $form_data ) {
  if ($form_id == 1234)
  {    
    // uncomment next line to inspect the entire post object using server logs.
    // error_log(print_r($_POST, true));
    $first = $_POST['first_name'];
    // ...
    // get the 3 other parameters from the POST object and add them here
    // ...
    wp_redirect( '/checkout?first=' . $first); // add all 4 parameters to the URL string here
    exit();
  }
}

Then in your woocommerce_checkout-fields filter, you should be able to access the URL parameters in the $_GET superglobal:

add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
    $first_name = isset($_GET['first']) ? $_GET['first'] : '';
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name';
    $fields['billing']['billing_first_name']['default'] = $first_name;
    return $fields;
} 

I'm not able to provide support for the woocommerce_checkout_fields API, because it is not part of our software. You should consult the WooCommerce documentation for more information there.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_form_action

https://docs.woocommerce.com/document/checkout-field-editor/

This support ticket is created 6 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#595391

This is in continuation of my previous support thread which got closed due to delay in reply.
Here's the original thread:
https://toolset.com/forums/topic/pass-cred-fields-and-custom-user-fields-data-to-woocommerce-checkout/

And here is the page:
hidden link

Tell us what you are trying to do?
I've setup a membership site that charges users to become member of the website.

Is there any documentation that you are following?
I've setup the site as per the below document.
https://toolset.com/documentation/user-guides/charging-payments-with-cred-to-register-users/

1. I've installed: CRED, WooCommerce and CRED Commerce.
2. Created a virtual product
3. Created a CRED New User Creation Form
4. On Submission, the page goes to WooCommerce checkout.

Is there a similar example that we can see?

What is the link to your site?
hidden link

The CRED User form was created using Auto-Generate User Form
Then I've had Custom User Field Group with following custom fields such as:
Mobile Number, Profile Photo, Date of Birth, etc

The problem, when I submit the form and user goes to WooCommerce checkout as expected, the WooCommerce again asks for:

1. First Name (Billing_First_Name)
2. Last Name (Billing_Last_Name)
3. Telephone Number (Billing_Phone)
4. Email Id (Billing_Email)

I would want the above 4 fields to be pre-filled with the CRED User form entered in the step 1:
First Name, Last Name, Mobile Number and Email Id.

Additional notes:
There is problem on similar lines
https://toolset.com/forums/topic/user-registration-in-woocommerce/#post-572163

But the solution is doesn't fit my problem as the user won't be created till the woocommerce checkout process is complete.

I'm sure the CRED saves the data somewhere in the interim before it goes to WooCommerce Checkout. The above functionality is very important from user experience perspective.

It will be great if someone from the team get dig deeper into this and provide a work-around.

#595411

But the solution is doesn't fit my problem as the user won't be created till the woocommerce checkout process is complete.
Modifying the Checkout page to use user-defined values isn't something we are able to help with here in the forums. CRED Commerce can add an item to your cart and redirect you to the standard Checkout page, but not modify the Checkout page programmatically. These fields on the Checkout page are controlled by WooCommerce, and the values displayed in the fields are determined by WooCommerce code. You'll need to override these values somehow, and I'm not sure if or how it can be done.

If you're able to determine how to predefine some values for these fields without a User already created in the database, I'll be glad to take a look and determine what we can do to help you access user-supplied values from the CRED Commerce form on the Checkout page. You may need to ask for assistance in the WooCommerce support forums to determine how to predefine these values, using PHP, without a current User saved in the database yet.

If you are able to contact WooCommerce support, I would ask them:
"How can I use PHP to predefine the values for first name, last name, email, and telephone number inputs on the Checkout page if the current user is a guest? Are any filters available to populate these inputs? Can you provide sample PHP code?"

If you can provide me with some sample code, I'll take a look.

#595487

Hi Christian,

It is very much possible to manipulate default WooCommerce checkout values using a filter.

Here's a sample code and reference site:

add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
    $user = wp_get_current_user();
    $first_name = $user ? $user->user_firstname : '';
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name';
    $fields['billing']['billing_first_name']['default'] = $first_name;
    return $fields;
}

Source: https://stackoverflow.com/questions/39922747/woocommerce-set-billing-field-value

Although the above code is for registered users, all we need to now do is fetch the values temporarily saved by CRED form before it creates user. I'm sure CRED must be holding this values somewhere, either as a draft post or something equivalent.

I trust my logic make sense.

#595698

Here's the documentation for the CRED Commerce API cred_commerce_form_action:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_form_action

Within the callback function, you will have access to anything captured by the form in the $_POST superglobal. You can use that information to construct a checkout URL with custom URL parameters, then trigger a redirect to the custom URL - something like

yoursite.com/checkout?first=John&last=Smith&address=123&email=abc

Then in your filter, you can access those variables from the $_GET superglobal. Here's a very simple example:

add_action('cred_commerce_form_action', 'my_commerce_form_action',10,4);
function my_commerce_form_action( $action, $form_id, $post_id, $form_data ) {
  if ($form_id == 1234)
  {    
    // uncomment next line to inspect the entire post object using server logs.
    // error_log(print_r($_POST, true));
    $first = $_POST['first_name'];
    // ...
    // get the 3 other parameters from the POST object and add them here
    // ...
    wp_redirect( '/checkout?first=' . $first); // add all 4 parameters to the URL string here
    exit();
  }
}

Then in your filter, you should be able to access the URL parameters in the $_GET superglobal:

add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
    $first_name = isset($_GET['first']) ? $_GET['first'] : '';
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name';
    $fields['billing']['billing_first_name']['default'] = $first_name;
    return $fields;
}
#597302
Screen Shot 2017-12-09 at 12.59.07 pm.png

Dear Christian,

This worked like a charm. Exactly what I wanted to accomplish. PFA screenshot. This was a very important missing piece for this project as well as future projects that I plan to work on CRED+CRED Commerce.

Thanks once again. Really appreciate it.

#602715

Thanks for this. I encountered this exact same issue today. It feels like it's a two step process - two forms rather than just one. Does CRED have to be used because WooCommerce is unable to create users of the required role type? e.g. only Customer.

It'd be useful to document exactly how you implemented this step by step for those that don't know how to do it - please?