• Resolved serhiishevchenko

    (@serhiishevchenko)


    Hey,
    I used this code for adding custom checkout field:

    /**
     * 
     * Create FB name as a custom field upon registration or checkout
     * 
     * @param $fields - fields already being registered
     * @param $screen - either checkout or registration screen
     * @return $fields - array with added field
     */
    function add_fb_name_lifterlms ( $fields , $screen ) {
    	if( strcmp( $screen , 'checkout' ) == 0 ||
    		strcmp( $screen , 'registration' ) == 0) {
    		$fb_name = array(
    			'columns' => 12,
    			'id' => 'llms_fb_name',
    			'label' => __('Ваш E-mail в Фейсбук (для доступа к группе)', 'lifterlms'),
    			'last_column' => false,
    			'required' => true,
    			'type' => 'text'
    		);
    		array_push($fields, $fb_name);
    	}
    	return $fields;
    }
    add_filter( 'lifterlms_get_person_fields', 'add_fb_name_lifterlms', 10, 2);
    /**
     *
     * Validate FB Name
     *
     * FB name should be at least 2 characters long
     *
     * @param $validated - current validation status
     * @param $data -  data being passed for validation
     * @param $screen - $screen should be registration or checkout
     * @return $validated - whether or not the company is valid
     */
    function validate_fb_name( $validated , $data, $screen ){
    	if( strcmp( $screen , 'checkout' ) == 0 ||
    	    strcmp( $screen , 'registration' ) == 0){
    		// Make sure fb name is at least characters long
    		if( strlen( $data[ 'llms_fb_name' ] ) < 2  ){
    			return new WP_Error( 'error-code', 'Неправильно введен E-mail в Фейсбук', 'my-text-domain' );
    		}
    	}
    	return $validated;
    }
    add_filter( 'lifterlms_user_registration_data' , 'validate_fb_name', 10 , 3 );
    add_filter( 'lifterlms_user_update_data' , 'validate_fb_name', 10 , 3 );
    /**
     * 
     * Save FB name to usermeta table
     * 
     * @param $person_id - id of user registering or checking out
     * @param $data -  data being passed through to be saved
     * @param $screen -  screen is either registration or checkout
     */
    function save_custom_fb_name( $person_id, $data , $screen ){
    	update_user_meta( $person_id, 'llms_fb_name', $data['llms_fb_name'], true);
    }
    add_action( 'lifterlms_user_registered', 'save_custom_fb_name', 10, 3);
    add_action( 'lifterlms_user_updated', 'save_custom_fb_name', 10, 3);

    It works great but I have a question:
    How it can be removed from registration page and used only on checkout page?

    Thanks.

    • This topic was modified 6 years, 8 months ago by serhiishevchenko. Reason: found answer for 1 of questions by myself

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter serhiishevchenko

    (@serhiishevchenko)

    I tried to change this section

    if( strcmp( $screen , 'checkout' ) == 0 ||
    		strcmp( $screen , 'registration' ) == 0)

    to this
    if( strcmp( $screen , 'checkout' ) == 0 )
    but it didn’t work for registration page. It still requires this custom field.

    @

    Just tested your code and updated it to only display on reg using the same method you suggested (which is the way) and it works fine for me: https://gist.github.com/thomasplevy/501c40cebd5d1b5b2b3733560b982078

    I’m not sure why it wouldn’t be working. Perhaps you have caching plugins installed? If so try clearing your cache.

    Thread Starter serhiishevchenko

    (@serhiishevchenko)

    OK, is there any difference between installed caching plugin and activated caching plugin? Because I have installed one but not activated. Can it still cause a problem?

    @serhiishevchenko,

    Installed but not active shouldn’t cause any issues but it’s hard to say. I have seem caching plugins persist their caches after deactivation due to the nature of the cache. You may have to reactivate, purge the cache, and then reenable. It’s hard to say and I’m sorry I really don’t know the answer here.

    Thread Starter serhiishevchenko

    (@serhiishevchenko)

    Ok. I’l’ try to figure it out. Thanks for help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Making not required custom field at checkout’ is closed to new replies.