Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created July 18, 2025 10:34
Show Gist options
  • Save xlplugins/01205f759262ac28fb798ff27af4cb6d to your computer and use it in GitHub Desktop.
Save xlplugins/01205f759262ac28fb798ff27af4cb6d to your computer and use it in GitHub Desktop.
Funnelkit Checkout: Display Custom Fields of checkout will be display under the tab my profile on my account page
class WFACP_Custom_field_On_Myaccount {
public $wfacp_id = 100; // Add your checkout page ID here
public $display_custom_fields = ['age', 'sport1', 'sport2', 'sport3', 'sport4']; // Add your custom field IDs here
// Add custom labels for your fields
public $field_labels = [
'age' => 'Age Range',
'sport1' => 'Primary Sport',
'sport2' => 'Secondary Sport',
'sport3' => 'Third Sport',
'sport4' => 'Fourth Sport'
];
public function __construct() {
// Add endpoint for 'My Profile' section under My Account
add_action('init', [$this, 'add_profile_endpoint']);
add_action('woocommerce_account_my-profile_endpoint', [$this, 'display_custom_fields']);
// Hook to save custom fields at checkout
add_action('woocommerce_checkout_update_user_meta', [$this, 'save_custom_field'], 10, 2);
// Hook to save custom fields when the user updates their profile
add_action('init', [$this, 'save_custom_field_account'], 10, 1);
// Add "My Profile" tab to My Account menu
add_filter('woocommerce_account_menu_items', [$this, 'add_my_profile_tab'], 10, 1);
// Default value handler for custom fields
add_filter('wfacp_default_values', [$this, 'set_default_value'], 9999, 2);
}
// Add "My Profile" tab to the My Account menu after the Dashboard tab
public function add_my_profile_tab($items) {
// Insert "My Profile" after the "Dashboard" tab
$new_items = [];
foreach ($items as $key => $value) {
$new_items[$key] = $value;
if ($key === 'dashboard') { // After the 'dashboard' menu item
$new_items['my-profile'] = 'My Profile'; // Add "My Profile" tab after Dashboard
}
}
return $new_items;
}
// Set default values for custom fields
public function set_default_value($field_value, $key) {
if (!is_user_logged_in() || !in_array($key, $this->display_custom_fields)) {
return $field_value;
}
$user_id = get_current_user_id();
$user_field_value = get_user_meta($user_id, $key, true);
if (!empty($user_field_value)) {
$field_value = $user_field_value;
}
return $field_value;
}
// Add custom endpoint for 'My Profile'
public function add_profile_endpoint() {
add_rewrite_endpoint('my-profile', EP_ROOT | EP_PAGES);
}
// Display custom fields in the "My Profile" section under the My Account tab
public function display_custom_fields() {
$user_id = get_current_user_id();
echo '<h3>My Profile</h3>';
echo '<form method="post" action="">'; // Form to handle saving custom fields
// Loop through the custom fields and display them as input fields
foreach ($this->display_custom_fields as $field) {
$field_label = isset($this->field_labels[$field]) ? $this->field_labels[$field] : ucfirst($field);
// For the 'age' field, display it as a dropdown
if ($field === 'age') {
$field_value = get_user_meta($user_id, $field, true);
?>
<p class="form-row form-row-wide">
<label for="<?php echo $field; ?>"><?php echo $field_label; ?></label>
<select name="<?php echo $field; ?>" id="<?php echo $field; ?>" class="input-text">
<option value="<18" <?php selected($field_value, '<18'); ?>>&lt;18</option>
<option value="18-24" <?php selected($field_value, '18-24'); ?>>18 - 24</option>
<option value="25-34" <?php selected($field_value, '25-34'); ?>>25 - 34</option>
<option value="35-44" <?php selected($field_value, '35-44'); ?>>35 - 44</option>
<option value="45-54" <?php selected($field_value, '45-54'); ?>>45 - 54</option>
<option value="55+" <?php selected($field_value, '55+'); ?>>55+</option>
</select>
</p>
<?php
} else {
// For other fields, display them as text inputs
$field_value = get_user_meta($user_id, $field, true);
?>
<p class="form-row form-row-wide">
<label for="<?php echo $field; ?>"><?php echo $field_label; ?></label>
<input type="text" name="<?php echo $field; ?>" id="<?php echo $field; ?>" value="<?php echo esc_attr($field_value); ?>" class="input-text">
</p>
<?php
}
}
// Add Save Changes Button
?>
<p class="form-row">
<button type="submit" class="button" name="save_profile_changes">Save Changes</button>
</p>
</form> <!-- End form -->
<?php
}
// Save custom field data to user meta when the user places an order during checkout
public function save_custom_field($user_id, $posted_data) {
foreach ($this->display_custom_fields as $field) {
if (isset($posted_data[$field])) {
$field_value = sanitize_text_field($posted_data[$field]);
update_user_meta($user_id, $field, $field_value);
}
}
}
// Save custom fields when the user updates their profile in the My Account page
public function save_custom_field_account() {
if (isset($_POST['save_profile_changes'])) {
$user_id = get_current_user_id();
foreach ($this->display_custom_fields as $field) {
if (isset($_POST[$field])) {
$field_value = sanitize_text_field($_POST[$field]);
update_user_meta($user_id, $field, $field_value);
}
}
}
}
}
// Initialize the class
new WFACP_Custom_field_On_Myaccount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment