Support » Plugin: YITH WooCommerce Affiliates » How to add new fields to the registration form?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hello there,

    You can add more fields to the registration field with a custom code you should add in the functions.php of your active theme, like this:

    if ( defined( 'YITH_WCAF' ) ) {
    	if ( ! function_exists( 'yith_wcaf_get_extra_fields' ) ) {
    		function yith_wcaf_get_extra_fields() {
    			return array(
    				'my_text_1' => array(
    					'type' => 'text',
    					'label' => 'My text 1',
    					'mandatory' => true,
    				),
    				'my_text_2' => array(
    					'type' => 'text',
    					'label' => 'My Text 2',
    					'mandatory' => true,
    				),
    			);
    		}
    	}
    
    	if ( ! function_exists( 'yith_wcaf_add_custom_fields' ) ) {
    		function yith_wcaf_add_custom_fields( $user = false ) {
    			$yith_wcaf_additional_fields = yith_wcaf_get_extra_fields();
    
    			$user_id = $user instanceof WP_User ? $user->ID : false;
    			$user_id = ( ! $user_id && is_user_logged_in() ) ? get_current_user_id() : $user_id;
    			$fields  = '';
    			$admin   = doing_action( 'show_user_profile' ) || doing_action( 'edit_user_profile' );
    
    			if ( $admin ) {
    				$fields .= "<hr/>
                <h3>Additional Affiliate Fields</h3>
                <table class='form-table'>";
    			}
    
    			foreach ( $yith_wcaf_additional_fields as $field => $opts ) {
    				if ( $user_id ) {
    					$value = get_user_meta( $user_id, 'yith_wcaf_' . $field, true );
    				} else {
    					$value = ! empty( $_POST[ 'yith_wcaf_' . $field ] ) ? $_POST[ 'yith_wcaf_' . $field ] : '';
    				}
    
    				$fields .= $admin ? '<tr><th>' : "<p class='form-row form-row-wide'>";
    				$fields .= "<label for='yith_wcaf_bank_number'>" . $opts['label'] . ': ' . ( $opts['mandatory'] ? "<span class='required'>*</span>" : '' ) . '</label>';
    				$fields .= $admin ? '</th><td>' : '';
    				$fields .= "<input type='" . $opts['type'] . "' class='input-text' name='yith_wcaf_" . $field . "' id='yith_wcaf_" . $field . "' value='$value' " . ( $opts['mandatory'] ? 'required' : '' ) . ' />';
    				$fields .= $admin ? '</td></tr>' : '</p>';
    			}
    
    			if ( $admin ) {
    				$fields .= '</table>';
    			}
    
    			echo $fields;
    		}
    
    		add_action( 'yith_wcaf_register_form', 'yith_wcaf_add_custom_fields' );
    		add_action( 'yith_wcaf_settings_form_after_payment_email', 'yith_wcaf_add_custom_fields' );
    		add_action( 'show_user_profile', 'yith_wcaf_add_custom_fields', 21, 1 );
    		add_action( 'edit_user_profile', 'yith_wcaf_add_custom_fields', 21, 1 );
    	}
    
    	if ( ! function_exists( 'yith_wcaf_register_custom_field' ) ) {
    		function yith_wcaf_register_custom_field( $param1, $param2 = false ) {
    			$yith_wcaf_additional_fields = yith_wcaf_get_extra_fields();
    
    			if ( doing_action( 'yith_wcaf_new_affiliate' ) ) {
    				$id = $param1;
    				$affiliate = YITH_WCAF_Affiliate_Handler()::get_instance()->get_affiliate_by_id( $id );
    				$user_id   = $affiliate['user_id'];
    			} elseif ( doing_action( 'yith_wcaf_save_affiliate_settings' ) ) {
    				$user_id = $param2;
    			} elseif ( doing_action( 'personal_options_update' ) || doing_action( 'edit_user_profile_update' ) ) {
    				$user_id = $param1;
    
    				if ( ! current_user_can( 'edit_user', $user_id ) ) {
    					return false;
    				}
    			}
    
    			if ( empty( $user_id ) ) {
    				return false;
    			}
    
    			foreach ( $yith_wcaf_additional_fields as $field => $opts ) {
    				if ( isset( $_REQUEST[ 'yith_wcaf_' . $field ] ) ) {
    					update_user_meta( $user_id, 'yith_wcaf_' . $field, sanitize_text_field( $_REQUEST[ 'yith_wcaf_' . $field ] ) );
    				}
    			}
    
    			if ( doing_action( 'yith_wcaf_save_affiliate_settings' ) ) {
    				return true;
    			}
    
    			return false;
    		}
    
    		add_action( 'yith_wcaf_new_affiliate', 'yith_wcaf_register_custom_field', 10, 1 );
    		add_action( 'yith_wcaf_save_affiliate_settings', 'yith_wcaf_register_custom_field', 10, 2 );
    		add_action( 'personal_options_update', 'yith_wcaf_register_custom_field', 10, 1 );
    		add_action( 'edit_user_profile_update', 'yith_wcaf_register_custom_field', 10, 1 );
    	}
    
    }

    I added two test fields, that you can find in this array of the previous code:

         return array(
    		'my_text_1' => array(
    			'type' => 'text',
    			'label' => 'My text 1',
    			'mandatory' => true,
    		),
    		'my_text_2' => array(
    			'type' => 'text',
    			'label' => 'My Text 2',
    			'mandatory' => true,
    		),
    	);

    You can add more fields as you wish.

    I hope it helps you.

    Have a great day!

    • This reply was modified 2 months, 1 week ago by YITHEMES.
    • This reply was modified 2 months, 1 week ago by YITHEMES.

    thanks. I’ll try.

    Plugin Author YITHEMES

    (@yithemes)

    Hello Leandro,

    We are going to close this topic because we don’t have notice about you.

    If you have any other problem, don’t hesitate to open a new topic and we’ll be happy to help you.

    Have a good day.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.