I'm trying to create a contact form within Craft CMS v2 that includes a subscribe button which will add the user's details to a MailChimp mail list. I'm currently trying to use this MailChimp Plugin and this Contact Form Plugin, though merging two plugins is proving to be quite difficult. Ideally this form would submit via AJAX. I'm hoping someone on here has done something similar using Craft before either with or without a plugin. Thanks.
Asked
Active
Viewed 514 times
1 Answers
0
Description: Subscribe users to Mailchimp lists in Craft CMS
source: https://github.com/boundstate/craft-mailchimp
Contact Form JavaScript Addition
In the contact form plugin you can just add a bit of javascript that calls the action of the Mailchimp form:
<form method="post" action="" accept-charset="UTF-8">
{{ csrfInput() }}
<input type="hidden" name="action" value="mailchimp/subscribe">
{{ redirectInput('subscribe/thanks') }}
<h3><label for="name">Your Name</label></h3>
<input id="name" type="text" name="mergeFields[NAME]" value="{{ subscription.mergeFields.NAME ?? '' }}" required>
<h3><label for="email">Your Email</label></h3>
<input id="email" type="email" name="email" value="{{ subscription.email ?? '' }}" required>
{{ subscription.getErrors('email')|join() }}
<input type="hidden" name="tags[]" value="Tag 1">
<input type="hidden" name="tags[]" value="Tag 2">
<input type="submit" value="Subscribe">
</form>
Send Controller
source https://github.com/craftcms/contact-form/blob/v2/src/controllers/SendController.php
In the send controller you would use the get Body function to get the checkbox value then call the controller for Mailchimp e.g. mailchimp/subscribe passing the email and name. You would have to do it in a post request using the above mailchimp plugin.
https://github.com/boundstate/craft-mailchimp/blob/master/src/controllers/SubscribeController.php
Or expand the subscribe controller to allow it to be called within the send controller.

rhand
- 1,176
- 4
- 17
- 45

Developer1
- 1
- 3
-
Will new updates of the contac form plugin not just overwrite these changes suggested? – rhand Aug 28 '21 at 00:35
-
Yes but just don't update the plugin.. Or create your own instance of the plugins. – Developer1 Aug 29 '21 at 00:25