Support » Plugin: Gravity Forms Constant Contact » Is Anyone Here Successfully Mapping the State to Constant Contact?

  • Resolved schmickler83

    (@schmickler83)


    If there is anyone here in the support forum that is successfully mapping the State over to Constant Contact, would you mind sharing how you did it? I have all the address fields sending info to CC, except the State, it does not transfer. I really would appreciate any information you can share. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • darrenkeoughball

    (@darrenkeoughball)

    it seems like you need to use the statecode which will resolve to state name in CC.

    schmickler83

    (@schmickler83)

    Hey thanks for responding. Yes, I tried using the statecode. It still does the same thing as using the State as a word. Always the same…. When you look in Constant Contact, at the list view which shows everyone’s name, email, city and state…. the state is blank, empty. But when you look at the individual contact record which shows complete address, the state is there.

    I asked Constant Contact and of course, they do not help as this plugin does not belong to them.

    I ran into this same issue, and delved into the plugin code to see what was going on. Ultimately, I found that if you’re using the US-configured version of the GravityForms address field, the state and country fields are set to the full name of their respective values.

    For example, if you select Texas, the constant contact plugin will pull “Texas” for the state, and “United States” for the country. This happens regardless whether you map it to state_name or state_code, and country_name or country_code.

    Unfortunately, mapping those values to state_name or country_name do nothing on the Constant Contact site. No translation, they are simply blanked out. So for my form, state_name and country_name serve no purpose and should never be mapped. And of course, if you map them to state_code and country_code, they are invalid and will result in either silent add-failure or blanked-out state/country in the CC contact entry.

    My fix for this is to never use state_name or country_name AND to replace function export_feed (constant_contact.php, line 1170) with this:

    
    public static function export_feed($entry, $form, $feed, $api){
        $email_field_id = $feed["meta"]["field_map"]["email_address"];
        $email = $entry[$email_field_id];
        $merge_vars = array('');
        foreach($feed["meta"]["field_map"] as $var_tag => $field_id){
    
        $field = RGFormsModel::get_field($form, $field_id);
        $field_type = RGFormsModel::get_input_type($field);
    
        if($field_id == intval($field_id) && $field_type == "address") {
            //handling full address
            $merge_vars[$var_tag] = self::get_address($entry, $field_id);
        } elseif( $var_tag == "country_code" && $field_type == "address" ) {
            // translate the country name from the GF address field into a country code
            $merge_vars[$var_tag] = GFCommon::get_country_code(trim($entry[$field_id]));
        } elseif( $var_tag == "state_code" && $field_type == "address" ) {
            // assuming that anything longer than two characters is a state name rather than a state code
            if(strlen(trim($entry[$field_id])) > 2) { 
                // translate the state name from the GF address field into a state code. 
                // GFCommon::get_us_state_code returns the original argument if it doesn't exist, so this is safe for non-US states
                $merge_vars[$var_tag] = GFCommon::get_us_state_code(trim($entry[$field_id]));
            } else {
                // Just set it.  If its a valid two-letter abbreviation, great.  If not, oh well!  GIGO.
                $merge_vars[$var_tag] = $entry[$field_id];
            }
        } elseif( $field_type == 'date' ) {
            $merge_vars[$var_tag] = apply_filters( 'gravityforms_constant_contact_change_date_format', $entry[$field_id] );
        } else {
            $merge_vars[$var_tag] = $entry[$field_id];
        }
    }
    

    Additionally, in order to keep my change in place, I’ve disabled update notifications for this plugin.

    All-in-all, I don’t think this is a terribly good fix for state_code, because it ignores Canada (there are three type settings for GF Address Field: International, United States, and Canadian). Canadian provinces won’t get translated to their two-digit abbreviations. Also, for International, the state field is a free-text field. I sort of addressed that by testing the length of the state in the above code snippet, but its not terribly robust.

    Also, I can foresee problems with International form visitors. How do you know which to set? state_code or state_name? And even if you do, will Constant Contact handle state_name correctly? In my US-specific testing, it did not.

    You could possibly set both state_name AND state_code, cross your fingers and hope it submits (however, I came across an old bug report from 2008 at Constant Contact’s API v1 discussion forums where you cannot set both and expect good results–I’m not certain if it was ever corrected). See https://community.constantcontact.com/t5/Developer-Support-ask-questions/Error-when-updating-a-Contact-with-StateCode/td-p/20264

    I found that if you’re using the US-configured version of the GravityForms address field, the state and country fields are set to the full name of their respective values

    You could tell gravity forms to pass the proper state abbreviations for the address field using this.

    Or build out the form with it’s own state field (not the full address field) using:

    
    
    Alabama|AL
    Alaska|AK
    Arizona|AZ
    Arkansas|AR
    California|CA
    Colorado|CO
    Connecticut|CT
    Delaware|DE
    District of Columbia|DC
    Florida|FL
    Georgia|GA
    Guam|GU
    Hawaii|HI
    Idaho|ID
    Illinois|IL
    Indiana|IN
    Iowa|IA
    Kansas|KS
    Kentucky|KY
    Louisiana|LA
    Maine|ME
    Maryland|MD
    Massachusetts|MA
    Michigan|MI
    Minnesota|MN
    Mississippi|MS
    Missouri|MO
    Montana|MT
    Nebraska|NE
    Nevada|NV
    New Hampshire|NH
    New Jersey|NJ
    New Mexico|NM
    New York|NY
    North Carolina|NC
    North Dakota|ND
    Ohio|OH
    Oklahoma|OK
    Oregon|OR
    Pennsylvania|PA
    Puerto Rico|PR
    Rhode Island|RI
    South Carolina|SC
    South Dakota|SD
    Tennessee|TN
    Texas|TX
    Utah|UT
    Vermont|VT
    Virginia|VA
    Washington|WA
    West Virginia|WV
    Wisconsin|WI
    Wyoming|WY
    

    You could also ask first which zone (country) for a conditional set of address fields and build out the fields per either above.

    Then send that as the zone code which is fairly standard use for json based API’s.

    Yup, that’s a a solution. When I initially started troubleshooting this issue, I wasn’t aware what the problem was. If I had known that before I put in the work to determine the source of the problem, I would have done it your way. But perhaps this information can help the plugin developer come up with a proper fix for this problem.

    Also, I realized I left out part of the export_feed function in my last post. Here’s my full, modified export_feed function:

    
        public static function export_feed($entry, $form, $feed, $api){
            $email_field_id = $feed["meta"]["field_map"]["email_address"];
            $email = $entry[$email_field_id];
            $merge_vars = array('');
            foreach($feed["meta"]["field_map"] as $var_tag => $field_id){
    
                $field = RGFormsModel::get_field($form, $field_id);
    			$field_type = RGFormsModel::get_input_type($field);
    
                if($field_id == intval($field_id) && $field_type == "address") {
                	//handling full address
                    $merge_vars[$var_tag] = self::get_address($entry, $field_id);
    			} elseif( $var_tag == "country_code" && $field_type == "address" ) {
                    // translate the country name from the GF address field into a country code
                    $merge_vars[$var_tag] = GFCommon::get_country_code(trim($entry[$field_id]));
                } elseif( $var_tag == "state_code" && $field_type == "address" ) {
                    // assuming that anything longer than two characters is a state name, as returned by the "US" version of the address field
                    if(strlen(trim($entry[$field_id])) > 2) { 
                        // translate the state name from the GF address field into a state code. 
                        // GFCommon::get_us_state_code returns the original argument if it doesn't exist, so this is safe for non-US states
                        $merge_vars[$var_tag] = GFCommon::get_us_state_code(trim($entry[$field_id]));
                    } else {
                        // Just set it.  If its a valid two-letter abbreviation, great.  If not, oh well!  GIGO.
                        $merge_vars[$var_tag] = $entry[$field_id];
                    }
                } elseif( $field_type == 'date' ) {
    				$merge_vars[$var_tag] = apply_filters( 'gravityforms_constant_contact_change_date_format', $entry[$field_id] );
                } else {
                    $merge_vars[$var_tag] = $entry[$field_id];
    			}
            }
    
            $retval = $api->listSubscribe($feed["meta"]["contact_list_id"], $merge_vars, "html");
    
           if( !is_wp_error( $retval ) && !empty($retval)) {
               self::add_note($entry["id"], __('Successfully added/updated in Constant Contact.', 'gravity-forms-constant-contact'));
            } else {
    
                $error = '';
                if( is_wp_error( $retval ) ) {
                    $error = ': '.$retval->get_error_message();
                }
    
                self::add_note($entry["id"], __('Errors when adding/updating in Constant Contact', 'gravity-forms-constant-contact') . $error );
            }
        }
    
    Plugin Author Zack Katz

    (@katzwebdesign)

    This is fixed in Version 3.0 – thanks for sharing your solutions. Please test 3.0 and confirm that it works for you; it’s worked for me well.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is Anyone Here Successfully Mapping the State to Constant Contact?’ is closed to new replies.