SmartForms and Mailchimp API

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
johnboyuk1
Forum Members
Forum Members
Posts: 200
Joined: Mon Nov 26, 2018 3:09 pm

SmartForms and Mailchimp API

Post by johnboyuk1 »

Has anyone used these together ... and can give me any pointers?

Is there a way of processing parts of a SmartForm from so that the Mailchimp API is called and values are sent? Not sure if its possible in SmartForms ... would need to be some kind of handler in SmartForm where I can process PHP I assume, but I can't see a way of doing that...

Any guidance appreciated !
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3492
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: SmartForms and Mailchimp API

Post by velden »

IIRC you can use a handler with smarty, hence calling a UDT (or plugin) and passing parameters
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

Yes, you can process parts of a SmartForm in CMS Made Simple to call the Mailchimp API by using a UDT as a form handler:

1) Create a UDT:
Go to Extensions > User Defined Tags and create a new UDT. Name it send_to_mailchimp. Add the following PHP code to handle the Mailchimp API call:

Code: Select all

$api_key = 'your_mailchimp_api_key';
$list_id = 'your_list_id';
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$data_center = substr($api_key,strpos($api_key,'-')+1);
$url = 'https://' . $data_center . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';

.........
2) Modify the SmartForm to Call the UDT:
Add a hidden field to your SmartForm to specify the UDT handler. Update your form code like this:

Code: Select all

<input type="hidden" name="m1_udt" value="send_to_mailchimp">
3) Process the Form:

Code: Select all

{if isset($_POST.m1_udt) && $_POST.m1_udt == 'send_to_mailchimp'}
    {send_to_mailchimp}
{/if}
I personally like to use ajax but let me know how it goes
johnboyuk1
Forum Members
Forum Members
Posts: 200
Joined: Mon Nov 26, 2018 3:09 pm

Re: SmartForms and Mailchimp API

Post by johnboyuk1 »

oooh... thanks for this! Let me try it out!
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

it's possible that "m1_" will need to be "cntnt01" check {$actionid}
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3492
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: SmartForms and Mailchimp API

Post by velden »

I don't understand why to use the hidden field. Could you explain it?
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

I guess it's not necessary if you want to trigger the UDT on every submission.
johnboyuk1
Forum Members
Forum Members
Posts: 200
Joined: Mon Nov 26, 2018 3:09 pm

Re: SmartForms and Mailchimp API

Post by johnboyuk1 »

What do I need to do if not using the hidden field? I would like to trigger the UDT on every submission ... thanks!
johnboyuk1
Forum Members
Forum Members
Posts: 200
Joined: Mon Nov 26, 2018 3:09 pm

Re: SmartForms and Mailchimp API

Post by johnboyuk1 »

Bit stuck on this (no surprise!) ... does all this code:

Code: Select all


<input type="hidden" name="m1_udt" value="send_to_mailchimp">
{if isset($_POST.m1_udt) && $_POST.m1_udt == 'send_to_mailchimp'}
    {send_to_mailchimp}
{/if}

go directly in the SmartForm design template ... for example just above where the code for the submit button is?

I tried the above but nothing seems to be happening

...grateful for any help!
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

two options you could try:

1) inside your form template:

{smtfm_form_errors assign='errors'}
{if !empty($errors)}
<ul class="error">
{foreach $errors as $err}
<li>{$err}</li>
{/foreach}
</ul>
{/if}
{send_to_mailchimp}
<form>

2) try putting the {send_to_mailchimp} in the "Form Output" tab in the "Smarty template for the final message to display to the user" box when you edit a form
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3492
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: SmartForms and Mailchimp API

Post by velden »

I think you should have a good understanding of the module to use it.
Have a look at the help, the Validation tab and the available Handlers.

Note that there's a 'POST form results to a URL/WebHook' handler, a 'Stop processing dispositions based on a condition' (it allows Smarty) and a 'Validate via Smarty' validation option.
I expect you can choose one to do what you want.
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

"Stop processing dispositions based on a condition" seems to be also an option. It will not give you the thank you message :)
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3492
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: SmartForms and Mailchimp API

Post by velden »

Well, if you make sure your Smarty code doesn't output anything on success, other dispositions will continue.
PLUS, as stated in the description, the final 'disposition' will be done anyway. I think that makes it possible to show the 'thank you message' regardless of the outcome of the earlier dispositions.
Last edited by velden on Tue May 21, 2024 9:31 am, edited 1 time in total.
Reason: typo
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 117
Joined: Thu Mar 24, 2011 12:37 am

Re: SmartForms and Mailchimp API

Post by magallo »

i think the important thing is, we gave our new friend johnboyuk1 some material to work with.
johnboyuk1
Forum Members
Forum Members
Posts: 200
Joined: Mon Nov 26, 2018 3:09 pm

Re: SmartForms and Mailchimp API

Post by johnboyuk1 »

Yes - super helpful. I'm getting somewhere now I've got the full API code...I have something rudimentary working, I'll paste full Mailchimp API code when im done in case it's useful to anyone else. :)
Post Reply

Return to “Modules/Add-Ons”