type:
integration
system:
MailChimp
name:
Add customers to mailing list.
description:
Allow customers to 'opt in' to your MailChimp mailing list during checkout.
tag:
mailing list, emarketing
date:
2008-05-21
mailchimp:
1.2
developer:
http://themancan.com/

MailChimp

We have implemented a different approach for MailChimp. In place of the following snippet, we now have webhooks for Zapier (beta) feature in FoxyCart that allows you to pass the data from FoxyCart onto a whole stack of other services, including MailChimp, quickly and easily. Please see the “Zapier” section here. This page will remain for reference.

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Description

MailChimp is a high-octane sportscar for concocting and delivering permission-based email marketing campaigns. It's simple, powerful, and more fun than a barrel of… well, monkeys.

This script does the following:

  1. It receives and decodes the FoxyCart XML datafeed that is POSTed to the fc-mailchimp.php page as in described in http://wiki.foxycart.com/docs/datafeed
  2. Checks if your customer checked an “opt in” checkbox on your checkout form (see below for how to customize your checkout form).
  3. If they have checked “opt in”, then it uses the MailChimp API to subscribe them to the MailChimp list of your choice.

Requirements

  • PHP4 or later, built with the XML extensions (you've probably got 'em).
  • A MailChimp account.

Installation

  1. Set Up your MailChimp account.
  2. Download the latest .zip of the fox2chimp git repository on github. Alternatively, you may use git, if that's the way you roll. The goods are in
    fc-mailchimp.php

    Included for good measure are the PHP MailChimp API interface, a PHP4-compatible XML parser, RC4 decryption library, and a test harness.

  3. Upload all 5(!) files to a folder your web server. Customize this block at the top of fc-mailchimp.php:
    // ...
     
    $MailChimp_Auth = array(
    	'apikey' => 'put your API key here'		// Use an MailChimp API key to connect to MailChimp.
     
    );
     
    /**
     * Use a custom field during checkout?  If true, check for the presence of $Custom_Field below.
     * If false, always subscribe the customer.  Use wisely.
     */
    $Use_Custom_Field = true;
     
    $Custom_Field = 'Subscribe';    // Name of the custom "Opt In" field during checkout.
    $Custom_Field_Value = 'yes';    // The value of the custom field that indicates the customer's agreement.
     
    $Email_Format = 'html';         // The customer's preferred email format.
    $Send_Confirmation = true;      // If true, MailChimp will send a confirmation email to the customer.
     
    $List_Name = 'My Awesome List'; // The exact name of your mailing list.  No List ID required, we'll look it up.
     
    $key = 'CHANGE THIS TEXT to your own datafeed API key'; // EDIT: Use here your FoxyCart Datafeed API key visble under "Store-->Advanced Settings"
     
    // ...

Testing

  1. First, test your MailChimp settings using the script found here: http://wiki.foxycart.com/integration/mailchimp_test. Download that script and follow the instructions on that page to make sure you have the proper MailChimp API key and list name configured.
    1. Log into your FoxyCart admin and go to Store → Advanced.
    2. Check the checkbox to turn the datafeed on, copy your datafeed API key and copy it to your configuration (the $key variable above).
    3. Enter the URL to fc-mailchimp.php (e.g. http://your.domain.dom/path/to/fc-mailchimp.php) that you uploaded as described above.
    4. Enter the exact same data key that you entered above into fc-mailchimp.php.
    5. Save your settings.
  2. TEST! It should work, but give it a test transaction to make sure. Enter your datafeed key and the URL of your fc-mailchimp.php into test.xmldatafeed.php, then open http://your.domain.dom/path/to/test.xmldatafeed.php in your browser. You should get a page that contains only the word foxy in reply. If you get something else, double-check your datafeed keys in both files and, if all else fails, pop on by the forums.
  3. Don't panic. You're done!

The "Subscribe" Checkbox

FoxyCart 2.0+

If your store is on FoxyCart 2.0 or newer, a new template configuration option will automatically insert a newsletter subscribe checkbox as part of the checkout form. To enable that, simply navigate to your store's FoxyCart administration, and on the “configuration” page, enable the “Allow users to sign up for your newsletter” option.

Within the fc-mailchimp.php file - you'll need to update the $Custom_Field variable to be set to newsletter_subscribe, and the $Custom_Field_Value to 1 like this:

$Custom_Field = 'newsletter_subscribe';    // Name of the custom "Opt In" field during checkout.
$Custom_Field_Value = '1';    // The value of the custom field that indicates the customer's agreement.

FoxyCart 1.1 and older

Now you have a datafeed receiver all set up on your server, so it's time to let your customers opt in to your mailing list. To do that, we need to add a Custom Field to your checkout template, so that your customer sees a “Subscribe” checkbox alongside their cart contents. There are a lot of cool things possible custom fields; find out more at the wiki page on Custom Fields. Today we're only going to add a single checkbox to the checkout template. Open the template up in your editor (if it's hosted on your server) or edit it directly from the FoxyCart admin interface, and add the following block of code anywhere in the template:

^^custom_begin^^
 
<h2>Newsletter</h2>
 
<fieldset id="fc_custom_fields">
	<legend>Newsletter</legend>
	<div class="fc_inner">
		<div>
			<h4>Subscribe</h4>
			<input type="checkbox" name="Subscribe" value="yes" checked="checked" />
			<label for="Subscribe">Leave this box checked to receive occasional updates about our products. We will not share your email address, and you can unsubscribe at any time.</label>
 
		</div>
	</div><!-- .fc_inner -->
</fieldset><!-- #fc_custom_fields -->
 
^^custom_end^^

You can put the code at the end of the file, or at the beginning – it doesn't matter. FoxyCart will move the <fieldset> into the checkout form when it loads your checkout page, so it can even be after the </body> and </html> tags. Replace “Newsletter” and “occasional updates about our products” with whatever's appropriate for your mailing list.

Save your changes to the checkout template in the FoxyCart admin. Test by adding a product to your cart and going to checkout – you should have a shiny new “Subscribe” checkbox, all primed and ready for your customers to use.

Code

Site Tools