Last active
June 18, 2021 10:34
-
-
Save denisbaranov/f07a409b59289a0f70f7092db0319d9f to your computer and use it in GitHub Desktop.
This example shows how to add custom Account tab with WooCommerce fields in Ultimate Member.
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
<?php | |
/** | |
* This example shows how to add custom Account tab with WooCommerce fields in Ultimate Member. | |
* See the article https://docs.ultimatemember.com/article/1504-how-to-add-custom-woocommerce-fields-to-account | |
* | |
* This example adds the tab 'wc_custom' that contains the field 'wc_custom_01'. You can add your own tabs and fields. | |
* Important! Each account tab has an unique key. Replace 'wc_custom' to your unique key. | |
* | |
* You can add this code to the end of the file functions.php in the active theme directory. | |
* | |
* Ultimate Member documentation: https://docs.ultimatemember.com/ | |
*/ | |
/** | |
* Add custom tab | |
* | |
* @param array $tabs | |
* @return array | |
*/ | |
function um_account_custom_tab_wc( $tabs ) { | |
$tabs[ 290 ][ 'wc_custom' ] = array( | |
'icon' => 'um-faicon-asterisk', | |
'title' => __( 'WC Custom', 'um-woocommerce' ), | |
'submit_title' => __( 'Save', 'um-woocommerce' ), | |
'custom' => true, | |
); | |
return $tabs; | |
} | |
add_filter( 'um_account_page_default_tabs_hook', 'um_account_custom_tab_wc', 100 ); | |
/** | |
* Add custom tab content | |
* | |
* @global WC_User $current_user | |
* @param string $output | |
* @param array $args | |
* @return string | |
*/ | |
function um_account_custom_tab_content_wc( $output = '', $args = array() ) { | |
global $current_user; | |
ob_start(); | |
echo '<div class="um-woo-form um-wc_custom">'; | |
/** Render field 'wc_custom_01', start */ | |
$key = 'wc_custom_01'; | |
echo '<div class="um-field" data-key="' . $key . '">'; | |
woocommerce_form_field( $key, array( | |
'id' => $key, | |
'type' => 'text', | |
'label' => 'Custom field label', | |
'description' => 'Custom field description', | |
'placeholder' => 'Custom field placeholder', | |
'class' => array(), | |
'custom_attributes' => array(), | |
'required' => true, | |
'default' => '', | |
), $current_user->$key ); | |
if( UM()->form()->has_error( 'wc_custom_01' ) ){ | |
echo '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . UM()->form()->errors[ 'wc_custom_01' ] . '</div>'; | |
} | |
echo '</div>'; | |
/** Render field 'wc_custom_01', end */ | |
echo '</div>'; | |
$output .= ob_get_clean(); | |
return do_shortcode( $output ); | |
} | |
add_filter( 'um_account_content_hook_wc_custom', 'um_account_custom_tab_content_wc', 20, 2 ); | |
/** | |
* Validate custom fields | |
* | |
* @param array $post_args | |
* @return type | |
*/ | |
function um_account_custom_tab_errors_wc( $post_args ) { | |
if( !isset( $post_args[ 'um_account_submit' ] ) ) { | |
return; | |
} | |
/** Validate field 'wc_custom_01' */ | |
if( isset( $post_args[ 'wc_custom_01' ] ) && ( strlen( $post_args[ 'wc_custom_01' ] ) < 3 || 99 < strlen( $post_args[ 'wc_custom_01' ] ) ) ) { | |
UM()->form()->add_error( 'wc_custom_01', __( 'This field length should be between 3 and 99', 'ultimate-member' ) ); | |
} | |
} | |
add_action( 'um_submit_account_errors_hook', 'um_account_custom_tab_errors_wc', 20 ); | |
/** | |
* Save custom fields | |
* | |
* @param int $user_id | |
* @param array $changes | |
*/ | |
function um_account_custom_tab_submit_wc( $user_id, $changes ) { | |
/** Save field 'wc_custom_01' */ | |
if( isset( $_POST[ 'wc_custom_01' ] ) && !UM()->form()->has_error( 'wc_custom_01' ) ) { | |
update_user_meta( $user_id, 'wc_custom_01', $_POST[ 'wc_custom_01' ] ); | |
} | |
} | |
add_action( 'um_after_user_account_updated', 'um_account_custom_tab_submit_wc', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment