Mailchimp for Modx with Checkbox

Using Formit for a simple contact form with a message and email input field, I intend to add a simple checkbox for letting my users optionally subscribe to a mailchimp newsletter in addition to sending me a message trough the contact form via formit. I noticed that there is an extra “Mailchimp for modx”. Is it somehow possible to combine this extra with formit to also suscribe to my newsletter only if the checkbox is active but send the formit message anyways or do I have to use Formit and Mailchimp for modx separately?

I hope I am making sense. Upon form submission:

  • if checkbox “newsletter” is not active, only send message (Formit)
  • if checkbox “newsletter” is active, send message via formit AND subscribe to mailchimp newsletter (Formit and Mailchimp for modx)

Do you mean this extra?

https://extras.modx.com/package/mailchimpforformit

If so, then you should be able to use the properties &mailchimpSubscribeField (and &mailchimpSubscribeFieldValue) like in the example on Github:

There’s also the Subscribe extra, which puts users into a usergroup. I don’t think it would work with a checkbox, but you could put a link to a custom “subscribe” form, like the “Become a Subscriber” link on this page. The subscribe form is configurable through a Tpl chunk.

Guys, thanks a lot for your input. I will have a look at it and try to make it work with the mailchimpforformit plugin. As bob pointed out, I fear it won’t be working with a simple checkbox but we will see.

Hi dakor2022, did you get this implementation to work? I want to do something similar where I store the form as normal but if a user ticks the subscribe checkbox a fomit hook will also the subscribe the name and email address to a Mailchimp list as per the documentation in mChimpX. If not, how did you do it in the end? Thanks

Hi, I solved it but not by using an existing modx extra. Instead, I wrote my own custom snippet which I inserted into my formit form as a prehook, prior to the execution of the mail hook. Since the website is no longer online, I unfortunately do not have the source code any more. This is what the snippet was basically doing:

  • checking if the “Subscribe to newsletter” checkbox is checked, if so, continue
  • gather all data from the form (name, mail, …)
  • execute a curl request to a mailchimp api endpoint and add the data to your newsletter database

In sum, that really was just about 40 lines of code. Hope it helps.

Maybe this script helps?

<?php

// Snippet FormIt Hook: fiMailchimpSubscribe

if ($hook->getValue("field_check") != 'value') {
    // send mail
} else {
    // add to mailchimp
    // $ mkdir core/components/mailchimpapi/
    // $ composer require mailchimp/marketing
    require_once(MODX_CORE_PATH . "components/mailchimpapi/vendor/autoload.php");    
    $email = $hook->getValue("field_email");
    $mailchimp = new MailchimpMarketing\ApiClient();    
    $mailchimp->setConfig([
        // https://mailchimp.com/developer/marketing/guides/quick-start/#generate-your-api-key
        "apiKey" => "****-us1",
        // see your login url or apiKey: https:// >> us1 << .admin.mailchimp.com/account/api/
        "server" => "us1"
    ]);
    // {listid}: see Audience > List > Settings > Audience name and defaults > Audience ID
    $response = $mailchimp->lists->setListMember("{listid}", md5(strtolower($email)), [
        "email_address" => $email,
        "status_if_new" => "subscribed",
        "merge_fields" => [
            "FNAME" => $hook->getValue("field_contactperson")
        ],
        "tags" => [
            "formit"
        ]
    ]);
}