An open API service indexing awesome lists of open source software.

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

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') %}

{{ craft.app.session.getFlash('notice') }}


{% elseif craft.app.session.hasFlash('error') %}

{{ craft.app.session.getFlash('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 %}
```