2

I would like to automatically subscribe all newly registered users to a MailChimp mailing list.

5

Install the MailChimp Subscribe plugin and add the following users.onActivateUser event listener to your site’s Business Logic plugin.

craft()->on('users.onActivateUser', function(Event $event) {
    $user = $event->params['user'];

    if (!$user->admin) {
        $result = craft()->mailchimpSubscribe->subscribe($user->email, '');

        if (!$result['success']) {
            Craft::log('Couldn’d subscribe to MailChimp list: '.print_r([
                'errorCode' => $result['errorCode'],
                'message' => $result['message'],
            ], true), LogLevel::Error);
        }
    }
});
5
  • Hi Calcs, this is interesting. But I am such a newbie that I have no clue where to put this?! Could you please tell me in which file of the business logic plugin I have to paste this? And where would any error messages show up? What happens if a user is already subscribed? Will Mailchimp still send activation Emails? Could you add a Name as well? – outline4 Jun 3 '17 at 22:32
  • You’d add that event listener to your plugin main class (BusinessLogic.php) init() method, @outline4. For your other questions please refer to the Mailchimp plugin documentation or ask a new question here using the tag plugin-mailchimp. – carlcs Jun 4 '17 at 21:30
  • Error messages would be logged to your craft.log file with the above code, but you can do whatever you want in the case of $result->success returning a false value. – carlcs Jun 4 '17 at 21:35
  • Thanks.. Man... I can't find a BusinessLogic.php file, nor an init() method... Gosh... I feel so dumb... All I see is these 4 files: BusinessLogicPlugin.php, BusinessLogicController.php, BusinessLogicService.php, BusinessLogicVariable.php... and in them I can't find any init() stuff... Could you please retry to help me? I'm such a dushbag ;( – outline4 Jun 7 '17 at 15:52
  • Sorry I meant the BusinessLogicPlugin.php file, create that method there @outline4 – carlcs Jun 7 '17 at 23:35
1

Update to carlcs answer:

The new MailChimp Subscribe plugin sends an array as result instead of an object. So I had to go this way:

craft()->on('users.onActivateUser', function(Event $event) {
    $user = $event->params['user'];

    if (!$user->admin) {
        $result = craft()->mailchimpSubscribe->subscribe($user->email, '');

        if (!$result["success"]) {
            Craft::log('Couldn’d subscribe to MailChimp list: '.print_r([
                'errorCode' => $result["errorCode"],
                'message' => $result["message"],
            ], true), LogLevel::Error);
        }
    }
});
0
1

If it helps anyone else, full working code for this task as of 19/11/2019 in Craft 3:

Using the MailChimp Subscribe and Business Logic plugins, place the following near top of BusinessLogic.php:

// Make sure we have relevant Craft bits for use
use craft\services\Users;
use craft\events\UserEvent;

Then place the below subscription code within your public function init() {}:

// Add new user to Mailchimp on account activation event
Event::on(
  Users::class,
  Users::EVENT_AFTER_ACTIVATE_USER,
  function (UserEvent $event) {

    // get our user
    $user = $event->user;    

    // Grab easier reference to mailchimp-subscribe plugin
    $msPlugin = Craft::$app->plugins->getPlugin('mailchimp-subscribe');

    // Make sure plugin exists
    if ($msPlugin && $msPlugin instanceof \aelvan\mailchimpsubscribe\MailchimpSubscribe) {

      $listID = 'XXXXXXXXXX'; // mailchimp audience ID

      // Example data
      $result = $msPlugin->mailchimpSubscribe->subscribe($user['email'], $listID, [
        'email_type' => 'html',
        'language' => 'en',
        'vip' => false,
        'tags' => false,
        'merge_fields' => [
          'FNAME' => $user['firstName'],
          'LNAME' => $user['lastName']
        ]
      ]);

      // Handle outcome
      if (!$result['success']) {

        // Do something if failed (like output error to logs)

      } else {

        // Do something else if success...

      }
    }
  }
);

Thanks to all other contributors here - definitely got me moving in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.