Making Saved Fields Available To All User Accounts

Conditional Checkout Fields provides a way to save the information a customer enters into a conditional field at checkout to their user account. These fields and their related information will show up in the user's account area after the purchase for the user to modify. Think of it like the customers address information that is stored in WooCommerce by default. If a customer moves, they may want to change their address in their account with you before they are ready to make that next purchase. The same thing can apply to some of the information that is stored in their conditional fields.

It is possible that you may want a customer to be able to complete their profile before they even place their first order. This might include completing some of the information that they would put in a conditional field.

Set fields to be saved to the customer's account

There are two ways to cause the customer's input to be saved to their account after checking out.

Globally

The global option will cause all entries to every conditional field to be saved to the customer's account. This can be enabled on the plugin's main settings (WooCommerce > Conditional Fields > Settings tab).

Conditional WooCommerce Checkout Field Settings

Individually

If you only want certain fields to be saved to the customer's account, you can check the box in the individual conditional field's settings. Note that this setting has no effect if the global option is enabled (see above).

Conditional WooCommerce Checkout Fields Save To Account

Where does the information show up in the customer's account?

By default the conditional fields only show up in the customer's account if:

  • The conditional field's settings are set to save the customer's information.
  • The customer has completed a purchase and provided information in the conditional field.

Otherwise the conditional fields do not show up in the customer's account area.

Show conditional checkout fields in account area whether or not the customer has previously filled in the field

If you would like the conditional fields to show up in the customer's account area before they make a purchase, you can add a bit of code to your theme's functions.php file or a custom plugin.

add_action( 'admin_init', 'cwcfp_add_conditional_fields_to_all_users' );
function cwcfp_add_conditional_fields_to_all_users(){
	global $wpdb, $cwcfp_db_table;
	// Get all users on your site - this can be modified to only get users of a certain role if desired.
	$users			= get_users();
	// Get all Conditional Checkout Fields
	$fields			= $wpdb->get_results("SELECT * FROM " . $cwcfp_db_table . " ORDER BY id;");
	// Field IDs that should be created in the customer's profile. Change the IDs to match the fields you want to save.
	$save_fields	= array( 1, 2, 3 );
	// Loop through each user
	foreach ( $users as $user ) {
		// Loop through each field
		foreach ( $fields as $field ) {
			// Make sure this field is set to be saved in the field's settings
			if( 1 == $field->save_input ){
				// If the field is in the array of fields we want to save, proceed
				if( in_array( $field->id, $save_fields ) ){
					// Add the field to the user's profile, but only if it doesn't exist already so we don't override any existing information.
					add_user_meta( $user->ID, $field->field_title, array( 0 => '' ), true );
				}
			}
		}
	}
}