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.
  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.

    // mailchimp config
    //API Key - see
    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 = '';
    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.

    * @author shikhar kumar (
    * mailchimp controller, uses mailchimp api to update list through cron
    * for details see my blog
    require_once( DIR_ADDONS . 'my_changes/MCAPI.class.php' ) ;
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if($mode == 'update_list'){
     exit('API Key not set in config') ;
     exit('List Id not set in config') ;
     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) ;
     exit('No user to add') ;
     $batch = array() ;
    foreach($user_array as $ua) {
     $batch[] = array(
    $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 ""

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

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 *