Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brandonkramer/ff32a4042704fa2821616f29f5b03d8c to your computer and use it in GitHub Desktop.
Save brandonkramer/ff32a4042704fa2821616f29f5b03d8c to your computer and use it in GitHub Desktop.
Add and integrate your BuddyPress profile / xProfile fields into the WooCommerce checkout page. With these hooks you can add custom fields to your checkout page based on BuddyPress xProfile fields and let them get saved to the user after the checkout is complete. We use the xprofile_get_field function and field ID to get and save data.
/**
* Add BudyPress fields to the WooCommerce checkout page by using the xprofile_get_field function from BudyPress
*
* @see xprofile_get_field()
*/
add_filter( 'woocommerce_checkout_fields', function ( $checkout_fields ) {
// text field example
$bp_field = xprofile_get_field( '211' ); // Get BudyPress field with Field ID
$bp_field_wc_id = sanitize_title( 'bp_field_' . $bp_field->name ); // Field ID to identify through the WooCommerce checkout process
$checkout_fields[ 'billing' ][ $bp_field_wc_id ] = [
'type' => 'text',
'class' => [ 'form-row-wide', 'budypress-field', 'budypress-field-' . $bp_field->id ],
'label' => $bp_field->name,
'placeholder' => '',
'required' => $bp_field->is_required,
'default' => '',
];
// select box example
$bp_field = xprofile_get_field( '958' ); // Get BudyPress field with Field ID
$bp_field_wc_id = sanitize_title( 'bp_field_' . $bp_field->name ); // Field ID to identify through the WooCommerce checkout process
$bp_field_options = []; // select box options
foreach ( $bp_field->get_children() as $option ) $bp_field_options[ $option->name ] = $option->name; // Insert options from BuddyPress select box field
$checkout_fields[ 'billing' ][ $bp_field_wc_id ] = [
'type' => 'select',
'class' => [ 'form-row-wide', 'budypress-field', 'budypress-field-' . $bp_field->id ],
'label' => $bp_field->name,
'required' => $bp_field->is_required,
'options' => $bp_field_options
];
return $checkout_fields;
}
/**
* After checkout completion, sync and update the BuddyPress fields for the customer
*
* @see xprofile_get_field()
*/
add_action( 'woocommerce_checkout_update_user_meta', function ( $customer_id, $data ) {
// Update text field example
$bp_field = xprofile_get_field( '211' ); // Get BudyPress field by Field ID
$bp_field_wc_id = sanitize_title( 'bp_field_' . $bp_field->name ); // Field ID to identify through the WooCommerce checkout process
if ( isset( $data[ $bp_field_wc_id ] ) ) xprofile_set_field_data( $bp_field->id, $customer_id, $data[ $bp_field_wc_id ] );
// Update select box example
$bp_field = xprofile_get_field( '958' ); // Get BudyPress field by Field ID
$bp_field_wc_id = sanitize_title( 'bp_field_' . $bp_field->name ); // Field ID to identify through the WooCommerce checkout process
if ( isset( $data[ $bp_field_wc_id ] ) ) xprofile_set_field_data( $bp_field->id, $customer_id, $data[ $bp_field_wc_id ] );
}, 10, 2 );
@nyadna
Copy link

nyadna commented Mar 22, 2023

Hi there,

Thank you for sharing this code snippet with the community! I'm trying to implement it on my functions file but I'm getting a syntax error. I'm not a coder, so I'm not really sure how to correct this. Here's the screenshot with the error message:

image

It would be really helpful if you can indicate to me what to adjust. Thank you!

Stefania

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment