• Resolved magaworks

    (@magaworks)


    Hi, another challenge πŸ™‚
    Can I add First Name and Last Name separate fields to a single field in Mailchimp?

    With your code do you think this is right? The field “your-fname” is mandatory but “your-lastname” is not. So it might be empty.

    function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
        if ( isset( $cf7_variables['your-fname'] ) ) {
            $first-name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
            $last-lastname = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
            $fullname = $first-name . " " .  $last-name;
            $merge_variables['FNAME'] = $fullname;
        }
    }
Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @magaworks,

    I love a challenge πŸ™‚

    I think your code looks perfect except you can’t use hyphens in your PHP variable names. Change $first-name and $last-lastname to $first_name and $last_name and everything should work.

    The only issue is that you might have an extra space for people who didn’t fill out a last name. We could fix that if you’d like.

    Cheers,
    Kevin.

    • This reply was modified 4 years, 10 months ago by yikesitskevin.
    Thread Starter magaworks

    (@magaworks)

    Ok, can you help me fix the extra space?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Of course.

    function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
        if ( isset( $cf7_variables['your-fname'] ) ) {
            $first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
            $last_name = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
            $fullname = ! empty( $last_name ) ? $first_name . " " .  $last_name : $first_name;
            $merge_variables['FNAME'] = $fullname;
        }
    }

    In this snippet, if the last name is empty, we will only add the first name (and no space). If we have a last name we’ll include the first name + a space + the last name.

    Cheers,
    Kevin.

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Oh – make sure to return the $merge_variables array too!

    function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
        if ( isset( $cf7_variables['your-fname'] ) ) {
            $first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
            $last_name = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
            $fullname = ! empty( $last_name ) ? $first_name . " " .  $last_name : $first_name;
            $merge_variables['FNAME'] = $fullname;
        }
    
        return $merge_variables;
    }
    Thread Starter magaworks

    (@magaworks)

    Something lije this:

    if ( isset( $cf7_variables['your-fname'] ) ) {
            $first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
            $last_lastname = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
            $fullname = $first_name . " " .  $last_lastname;
    		if ( isset( $cf7_variables['your-lastname'] ) ) {
            $merge_variables['FNAME'] = $fullname;
    		} else {
    			$merge_variables['FNAME'] = $first_name;
    		}
        }
    Thread Starter magaworks

    (@magaworks)

    You beat me to it πŸ™‚

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    That would work but you want to check if the $cf7_variables['your-lastname'] is empty rather than if it’s set.

    So this line: if ( isset( $cf7_variables['your-lastname'] ) ) {

    Should be this: if ( ! empty( $cf7_variables['your-lastname'] ) ) {

    I am not exactly sure how CF7 passes the data but I think $cf7_variables['your-lastname'] will be set regardless of whether the user filled it out (i.e. it’s set but it’s empty).

    Thread Starter magaworks

    (@magaworks)

    You win. Your code is cleaner.
    But I am not a developer. I am just a fast learner and curious, self-taught.

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    I think you’re doing great and I only want to help πŸ™‚

    Thread Starter magaworks

    (@magaworks)

    Something is not working. It is not sending to mailchimp, althought the Contact Form is sent.
    I have Contact form 7 integration on and select the Newsletter list.
    Here is my final code:

    add_filter( 'yikes-mailchimp-contact-form-7', 'additional_contact_form_7_data', 10, 2 );
    function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
    if ( isset( $cf7_variables['your-fname'] ) ) {
            $first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
            $last_name = filter_var( $cf7_variables['your-lname'], FILTER_SANITIZE_STRING );
            $fullname = ! empty( $last_name ) ? $first_name . " " .  $last_name : $first_name;
            $merge_variables['FNAME'] = $fullname;
        }
    	if ( isset( $cf7_variables['company'] ) ) {
            $merge_variables['COMPANY'] = filter_var( $cf7_variables['company'], FILTER_SANITIZE_STRING );
        }
    	if ( isset( $cf7_variables['origem-lp'] ) ) {
            $merge_variables['ORIGEM'] = filter_var( $cf7_variables['origem-lp'], FILTER_SANITIZE_STRING );
        }
    	if ( isset( $cf7_variables['TOS-hiden'] ) ) {
            $merge_variables['TOS'] = filter_var( $cf7_variables['TOS-hiden'], FILTER_SANITIZE_STRING );
        }
        return $merge_variables;
    }
    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Can you enable debugging in the plugin’s settings and let me know if there’s an error showing there?

    Do you have the MERGE fields COMPANY, ORIGEM, and TOS in your Mailchimp list?

    Thread Starter magaworks

    (@magaworks)

    I have debug enabled and nothing is showing there.
    And yes I have those MERGE Fields in Mailchimp.

    Thread Starter magaworks

    (@magaworks)

    No fields are mandatory except email.

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Are you using our filter to make the integration single opt-in? If not, the subscription will be double opt-in. If you’re using a test email address, the double opt-in will never be confirmed. Could that be the problem?

    Thread Starter magaworks

    (@magaworks)

    Yes, I have this code:

    add_filter( 'yikes-mailchimp-checkbox-integration-body', 'set_integrations_to_single_optin', 10, 2 );
    function set_integrations_to_single_optin( $request_body, $integration_type ) {
    	$request_body['status_if_new'] = 'subscribed';
    	$request_body['status']        = 'subscribed';
    	return $request_body;
    }

    Just before the other one.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Adding first-name and last-name from contact for to a single Mailchimp Field’ is closed to new replies.