Last active
March 22, 2023 23:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
It would be really helpful if you can indicate to me what to adjust. Thank you!
Stefania