• Resolved kongb

    (@kongb)


    Hello,
    I have an opt-in checkbox on the WooCommerce My Account Login / Registration page that enables users to sign up for the newsletter when creating a customer account.

    The default version of the Woocommerce registration form only includes an e-mail field. However, I have added two more fields (first name and last name) to the form by adding the php from the below tutorial to my theme’s child theme functions.php

    https://www.cloudways.com/blog/add-woocommerce-registration-form-fields/

    I wonder if there is a way to add to this code so that the user input in the first name and last name fields on the My Account registration form, populates the First Name and Last Name fields in MailPoet if the user signs up to the newsletter via this form. At the moment the e-mail alias populates the first name field and the last name field is left blank.

    Any help is much appreciated.

    Thanks,
    Rikke

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there @kongb,

    Thanks for reaching out!

    When you’re registering a new user with your custom fields, do you see them in wp-admin > Users > click on the user you registered?

    MailPoet should grab the fields from the WordPress User data if they are there.

    Let me know how that goes!

    Thread Starter kongb

    (@kongb)

    Hi Elvira,

    Thanks for your reply. Yes the user-input in the First Name and Last Name custom fields on the registration form populates the First Name and Last Name fields in wp-admin > Users > new registered user. But the new subscriber’s first and last name is not transferred over to MailPoet. I have testet this a few times with the same result, The fields that are populated are :
    e-mail field: (new subscriber’s e-mail)
    First Name field: ( new subscribers username -ex. the email alias)
    Last Name: ( is blank -not populated)
    Lists: Populated with the name of the default mailing list.

    Below is the code I have used to create the custom fields on the registration page. I wonder if (with regard to MailPoet) any code might be missing in the section that saves the user input to the database. Ex. is it possible to add some code that will save the the first and last names in the relevant MailPoet table in the database (or is MP creating and displaying’Views’ of other database tables rather than storing actual data in tables?)

    Any help will be much appreciated.

    Kind regards
    Rikke

    /* ----Add extra fields to my account registration page----*/
    
    /*Add  custom fields to the frontend of WooCommerce Registration Form.*/
    
    function wooc_extra_register_fields() {?>
           <p class="form-row form-row-first">
           <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
           <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
           </p>
           <p class="form-row form-row-last">
           <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
           <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
           </p>
           <div class="clear"></div>
           <?php
     }
     add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
    
    /*Validate the added fields in the my account registration form*/
    function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    
          if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
    
                 $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
    
          }
    
          if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
    
                 $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
    
          }
             return $validation_errors;
    }
    
    add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
    
    /**
    * Save values in new My Account registration fields to database.
    */
    function wooc_save_extra_register_fields( $customer_id ) {
        if ( isset( $_POST['billing_phone'] ) ) {
                     // Phone input filed which is used in WooCommerce
                     update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
              }
          if ( isset( $_POST['billing_first_name'] ) ) {
                 //First name field which is by default
                 update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
                 // First name field which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
          }
          if ( isset( $_POST['billing_last_name'] ) ) {
                 // Last name field which is by default
                 update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
                 // Last name field which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
          }
    
    }
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
    • This reply was modified 3 years, 7 months ago by kongb.
    • This reply was modified 3 years, 7 months ago by kongb.
    Thread Starter kongb

    (@kongb)

    Hello again Elvira,

    I have figured out what the problem is. MailPoet populates the First Name field with the value written in the “Display name publicly as” field in the wp user profile ( Ex. the user Display Name). By default that field is populated with the username (ex. the email alias). I have finally figured a way to change the default Display Name from the username to the first name and last name, so the problem has sort of and almost been solved (The last name is still left blank). However it took me quite a while to find a php function that worked, so it would be a neat update for the future if MailPoet could automatically grab the first and last names from the wp-user profile and then provide a choice in the MailPoet admin settings between using username, first name and/or last name for greeting subscribers and customers.

    Kind regards,
    Rikke

    Hi there @kongb,

    I apologize for the late reply!

    I’m glad to hear that you were able to solve the issue with your custom registration forms.

    You’re also welcome to add it as a suggestion here https://feedback.mailpoet.com

    Our team will take it into account when discussing new features and improvements for the plugin ??

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Newsletter opt-in from WooCommerce My Account Registration page’ is closed to new replies.