boundstate / craft-mailchimp
Subscribe users to Mailchimp lists in Craft CMS
Installs: 256
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 13
Type:craft-plugin
Requires
- php: ^8.0.2
- craftcms/cms: ^4.0.0
- drewm/mailchimp-api: ^2.5
Requires (Dev)
- craftcms/rector: dev-main
- dev-master
- dev-craft-4
- v4.0.0
- v1.1.1
- v1.1.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/craftcms/cms-4.4.6.1
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-dependabot/npm_and_yarn/minimist-1.2.8
- dev-dependabot/npm_and_yarn/ansi-regex-5.0.1
- dev-dependabot/npm_and_yarn/trim-off-newlines-1.0.3
- dev-dependabot/npm_and_yarn/shelljs-0.8.5
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/lodash-4.17.21
- dev-dependabot/npm_and_yarn/handlebars-4.7.7
- dev-dependabot/npm_and_yarn/y18n-4.0.1
- dev-dependabot/npm_and_yarn/ini-1.3.8
- dev-dependabot/npm_and_yarn/standard-version-8.0.1
This package is auto-updated.
Last update: 2024-10-26 16:57:32 UTC
README
Subscribe users to Mailchimp lists in Craft CMS
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require boundstate/craft-mailchimp
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Mailchimp.
-
In the Control Panel, go to Settings → Plugins → Mailchimp and configure the plugin.
Usage
Your subscribe form template can look something like this:
<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>
The only required field is email
. Everything else is optional.
Redirecting after submit
If you have a redirect
hidden input, the user will get redirected to it upon successfully subscribing. The following variables can be used within the URL/path you set:
{email}
{mergeFields}
{tags}
For example, if you wanted to redirect to a subscribe/thanks
page and pass the user’s name to it, you could set the input like this:
{{ redirectInput('subscribe/thanks?name={mergeFields.NAME}') }}
In your subscribe/thanks
template, you can access URL parameters using craft.app.request.getQueryParam()
:
<p>Thanks for subscribing, {{ craft.app.request.getQueryParam('name') }}!</p>
Note that if you don’t include a redirect
input, the current page will get reloaded.
Flash messages & API errors
When a subscribe form is submitted, the plugin will set a notice
or success
flash message on the user session. You can display it in your template like this:
{% if craft.app.session.hasFlash('notice') %} <p class="message notice">{{ craft.app.session.getFlash('notice') }}</p> {% elseif craft.app.session.hasFlash('error') %} <p class="message error">{{ craft.app.session.getFlash('error') }}</p> {% endif %}
If the Mailchimp API returns an error, the plugin also sets the subscription.apiError
variable. You can display it in your template like this:
{% if subscription is defined and subscription.apiError %} <h3>{{ subscription.apiError.title }}</h3> <p>{{ subscription.apiError.detail }}</p> {% endif %}