Addon code to sync Cs-cart users to Mailchimp

This post will explain how to sync Cs-cart newly registered customers to your Mailchimp list on a daily basis.
mailchimp cs-cart integration

You can edit one of your existing addons.  If you don’t have an existing addon, do the changes in my_changes addon, just make sure you have it installed.  To illustrate I will do the changes in my_changes addon.

  1. Make sure you have the addon installed.  Verify from Administration->addons page.
    my_changes_installed
  2. Copy Mailchimp API php file to addons\my_changes\MCAPI.class.php
    Get the file
  3. Add config file and put your config parameters, create folder and files if not present.
    addons\my_changes\config.php

    <?php
    // mailchimp config
    //API Key - see http://admin.mailchimp.com/account/api
    define('MC_API_KEY', 'Your config');
    
    // Also, login to MC account, go to List, then List Tools, and look for the List ID entry
    define('MC_LIST_ID', 'List ID you want to update');
    
    //just used in xml-rpc examples
    $apiUrl = 'http://api.mailchimp.com/1.3/';
    
    define('MC_SYNC_PERIOD', 25*60*60) ; // little over 24 hrs to not miss anything on overlap
    
    ?>
    
  4. Create cs-cart controller mailchimp, create folder and files if not present.
    addons\my_changes\controllers\customer\mailchimp.php

    
    <?php
    /**
    * @author shikhar kumar (shikhar.kr@gmail.com)
    * mailchimp controller, uses mailchimp api to update list through cron
    * for details see my blog http://shikhar.me/blog
    */
    
    require_once( DIR_ADDONS . 'my_changes/MCAPI.class.php' ) ;
    
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    
    if($mode == 'update_list'){
    
     if(!MC_API_KEY){
     exit('API Key not set in config') ;
     }
    
     if(!MC_LIST_ID){
     exit('List Id not set in config') ;
     }
    
     if(!MC_SYNC_PERIOD){
     exit('Sync period not set in config') ;
     }
    
     $api = new MCAPI(MC_API_KEY);
    
     $since_timestamp = time() - MC_SYNC_PERIOD ;
    
     // get customers
     $user_array = db_get_array('SELECT user_id,title,firstname,lastname,email FROM ?:users WHERE user_type = "C" AND timestamp > ?i', $since_timestamp) ;
    
     if(empty($since_timestamp)){
     exit('No user to add') ;
     }
    
     $batch = array() ;
    
    foreach($user_array as $ua) {
     $batch[] = array(
     'EMAIL'=>$ua['email'],
     'FNAME'=>$ua['firstname'],
     'LNAME'=>$ua['lastname'],
     'TITLE'=>$ua['title']
     );
     }
    
    $optin = false; //yes, send optin emails
     $up_exist = false; // yes, update currently subscribed users
     $replace_int = false; // no, add interest, don't replace
    
    $vals = $api->listBatchSubscribe(MC_LIST_ID,$batch,$optin, $up_exist, $replace_int);
    
    if ($api->errorCode){
     echo "Batch Subscribe failed!<br/>";
     echo "code:".$api->errorCode."<br/>";
     echo "msg :".$api->errorMessage."<br/>";
     } else {
     echo "added: ".$vals['add_count']."<br/>";
     echo "updated: ".$vals['update_count']."<br/>";
     echo "errors: ".$vals['error_count']."<br/>";
     foreach($vals['errors'] as $val){
     echo $val['email_address']. " failed<br/>";
     echo "code:".$val['code']."<br/>";
     echo "msg :".$val['message']."<br/>";
     }
     }
    
    exit('<br/>complete') ;
    
     } // end update_list
    
    }
    
    ?>
    
    
  5. Now add a cron job to execute this controller daily.  For example if you want to execute it at 8 am daily.  The cron entry should be :
    0 8 * * * wget -O /dev/null "www.your-site.com/index.php?dispatch=mailchimp.update_list"
    

That’s it.  Now you no longer need to upload csv files to mailchimp ! Enjoy :)

Note : MC_LIST_ID to be specified in config file is not your list name but a alphanumeric string, see below
mailchimp_listid

Posted in CS-Cart, CS-Cart Addons
One comment on “Addon code to sync Cs-cart users to Mailchimp
  1. Jan Brun says:

    Will this also work for CS-Cart 4 ?

Leave a Reply

Your email address will not be published. Required fields are marked *

*