So You Wanna Synch Mailchimp and ZenCart?

So you’ve got a great e-com site going using Zencart and you’ve got customers registering every day. Zencart’s a good shopping cart but it’s a pretty crappy mailing list manager. So how do you keep your Zencart customer database in synch with your Mailchimp database without hacking the core code ie. create_account.php?

The Zencart team recently included notifiers and observers in a re-write. Using an observer makes an ideal solution to this particular dilemma.

First of all, head over to Mailchimp and pick up their PHP wrapper MCAPI.class.php. For want of a better place to put it just dump it into your Zencart install at includes/classes/observers/ while you won’t modify this file directly you will use it pretty shortly.

Now, create a new class in includes/classes/observers/ called class.subscriber.php this is the one that will send the subscription off to Mailchimp. The source code for this is below.

<?php
class subscriber extends base {

   function subscriber() {
     $this->attach($this, array('NOTIFY_MODULE_CREATE_ACCOUNT_ADDED_CUSTOMER_RECORD'));
   }

  function update(&$callingClass, $notifier, $paramsArray) {

  require_once 'MCAPI.class.php';

  $api = new MCAPI('yourapikeyhere');
  $listId = 'yourlistidhere';

  $my_email = $paramsArray['customers_email_address'];
  $merge_vars = array('FNAME'=>$paramsArray['customers_firstname'], 'LNAME'=>$paramsArray['customers_lastname'],
  'MMERGE6'=>date_format(date_create(),'Ymd'),
                      'INTERESTS'=>'');

  $retval = $api->listSubscribe( $listId, $my_email, $merge_vars,'html',false);

  }
}

?>

In short, we are listening out for the NOTIFY_MODULE_CREATE_ACCOUNT_ADDED_CUSTOMER_RECORD notification and hooking our class onto this. While I though the NOTIFY_MODULE_CREATE_ACCOUNT_SUCCESS would be a more logical notification this seemed to get called before the account had been written to the dbase and before the customer had been assigned an ID.

While the official Zencart spec says that $paramsArray is still a future placeholder, on my installation of Zencart the NOTIFY_MODULE_CREATE_ACCOUNT_ADDED_CUSTOMER_RECORD notification did also pass the $paramsArray with relevant info.

Once this class has been created we need to splice this into the autoloader chain. This is really quite easy. Go to /includes/autoloaders/ and create a file config.subscriber.php. This only has the two lines used to hook your new class into the application. Source code is below.


<?php

$autoLoadConfig[0][] = array('autoType'=>'class',
                              'loadFile'=> 'observers/class.subscriber.php');
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
                              'className'=>'subscriber',
                              'objectName'=>'subscriber');
?>

Sit back and watch your mailing list grow. It’s all kept in synch and you can have the best of both worlds! The best shopping cart and the best mailing list management provider.

This entry was posted in Zencart. Bookmark the permalink.

Comments are closed.