https://github.com/boundstate/craft-mailchimp
Subscribe users to Mailchimp lists in Craft CMS
https://github.com/boundstate/craft-mailchimp
Last synced: about 1 year ago
JSON representation
Subscribe users to Mailchimp lists in Craft CMS
- Host: GitHub
- URL: https://github.com/boundstate/craft-mailchimp
- Owner: boundstate
- License: mit
- Created: 2020-01-09T15:48:11.000Z (over 6 years ago)
- Default Branch: craft-4
- Last Pushed: 2023-05-26T13:56:24.000Z (about 3 years ago)
- Last Synced: 2025-02-14T17:43:16.244Z (over 1 year ago)
- Language: PHP
- Size: 402 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Mailchimp plugin for Craft CMS 4.x
Subscribe users to Mailchimp lists in Craft CMS
## Installation
To install the plugin, follow these instructions.
1. Open your terminal and go to your Craft project:
cd /path/to/project
2. Then tell Composer to load the plugin:
composer require boundstate/craft-mailchimp
3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Mailchimp.
4. In the Control Panel, go to Settings → Plugins → Mailchimp and configure the plugin.
## Usage
Your subscribe form template can look something like this:
```twig
{{ csrfInput() }}
{{ redirectInput('subscribe/thanks') }}
Your Name
Your Email
{{ subscription.getErrors('email')|join() }}
```
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:
```twig
{{ redirectInput('subscribe/thanks?name={mergeFields.NAME}') }}
```
In your `subscribe/thanks` template, you can access URL parameters using `craft.app.request.getQueryParam()`:
```twig
Thanks for subscribing, {{ craft.app.request.getQueryParam('name') }}!
```
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:
```twig
{% if craft.app.session.hasFlash('notice') %}
{% elseif craft.app.session.hasFlash('error') %}
{% 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:
```twig
{% if subscription is defined and subscription.apiError %}
{{ subscription.apiError.title }}
{{ subscription.apiError.detail }}
{% endif %}
```