We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36786 ☆ A M B ☆
    • 209 Posts
    I'm trying to create a snippet for formit that will submit the data in the form to mailchimp as a new subscriber. I'm just not very expirienced with PHP and I seem to be running into trouble. Every time I submit the form, it brings me back to the form and no user is added to the email ist. No error is generated in the log either. The formit2db/db2formit work perfectly, it's the addition of this snippet that messes it up (hence the packageName parameter).

    I took the examples from mailchimp's website and modified it for the snippet code so the basics SHOULD be right. Does anyone have any experience with the MailChimp API? I have never tried to create a snippet before so I'm new at this and may be messing up something really basic. It would make my life so much easier if I didn't have to regularly export mysql tables and import them into mailchimp.

    Examples of API use: http://apidocs.mailchimp.com/api/downloads/#examples

    
    $packageName = $scriptProperties['packageName']; 
    $packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
    $modelpath = $packagepath.'model/';
    /*for formit2db but it's the directory where I place the CAPI.class.php file */
    
    $volunteer = $hook->getValue('volunteer');
    if ($volunteer == 'Yes') {
    
    
    function storeAddress(){
    	
    
    	require_once($modelpath.'/MCAPI.class.php');
    	// grab an API Key from http://admin.mailchimp.com/account/api/
    	$api = new MCAPI('MyApiKey');
    	
    	// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    	// Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    	$list_id = "MyListID";
    
    	// Merge variables are the names of all of the fields your mailing list accepts
    	// Ex: first name is by default FNAME
    	// You can define the names of each merge variable in Lists > click the desired list > list settings > Merge tags for personalization
    	// Pass merge values to the API in an array as follows
    	$mergeVars = array('FNAME'=>$hook->getValue('name'),
    						'ADD1'=>$hook->getValue('address'),
    						'ADD2'=>$hook->getValue('address2'),
    						'CITY'=>$hook->getValue('city'),
    						'STATE'=>$hook->getValue('state'),
    						'ZIP'=>$hook->getValue('zip'),
    						'COUNTY'=>$hook->getValue('county');
    
    if($api->listSubscribe($list_id, $scriptProperties['email'];, $mergeVars) === true) {
    		// It worked!	
    		return true;
    	}else{
    		// An error ocurred, return error message	
    		$modx->log(modX::LOG_LEVEL_ERROR,'[FormIt] mailchimpAPI could complete the addition of user to list.');
        return false;
    	}
    } 
    else {
        return true;
    }
    					
    
      Ben Morrison
      Hacking templates to pieces since 2003
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Very helpful as to solve your issue, I know, but did you try mChimpX? http://modx.com/extras/package/mchimpx
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 36786 ☆ A M B ☆
        • 209 Posts
        Oh for the love of god, how in the world did I miss THAT?! Well now I feel like a schmuck, LOL. Thanks Mark, maybe I need a vacation...
          Ben Morrison
          Hacking templates to pieces since 2003
          • 36786 ☆ A M B ☆
          • 209 Posts
          Anyone know how I could get it to subscribe multiple emails?
            Ben Morrison
            Hacking templates to pieces since 2003
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            Call it several times? Or use Mailchimp's import utility on the site.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 36786 ☆ A M B ☆
              • 209 Posts
              I was able to call it several times, with some glitchyness but it's stable now. Thank you.
                Ben Morrison
                Hacking templates to pieces since 2003