Software Developer

MailChimp Subscription Checkbox for Easy Digital Downloads Checkout Page

This is an easy and free way to add a MailChimp subscription checkbox to the Checkout page in Easy Digital Downloads. This allows your EDD customers to sign up to your Newsletter, or any email list, at checkout. If the customer checks the box to subscribe to your newsletter, they will be automatically subscribed to your MailChimp email list when they checkout. That is, when they click the “Purchase” button.

This works with Easy Digital Downloads, a.k.a. EDD, which is a WordPress plugin.

All four of the code blocks go into your functions.

  1. The first code block will display the sign up checkbox on the EDD checkout page. The label next to the sign up check box provides all the long “fine print” to comply with certain privacy laws. It reads:

    Sign up for the COMPANY NAME newsletter. (Your email will be used to send you our updates. You can unsubscribe at any time using the link in our emails. For more details, review our privacy policy. We use MailChimp as our marketing automation platform. By checking this box, you acknowledge that your email address will be transferred to MailChimp for processing in accordance with their Privacy Policy and Terms.)

     

    You must add in the URL of your privacy policy page because this example simply has # as the URL. If you prefer, you can remove all the extra fine print, and just leave the first sentence. Be sure to change “COMPANY NAME” in the first sentence to your own company name, or simply “our newsletter.” This text is all found on line 10 of this first code block.

    /**
     * Display the signup checkbox on the EDD checkout screen
     */
    add_action( 'edd_purchase_form_before_submit', 'isa_checkout_subscribe_field', 100 );
    function isa_checkout_subscribe_field() {
    	ob_start(); ?>
    	<fieldset id="edd_mailchimp">
    		<p>
    			<input name="isa_edd_mailchimp_signup" id="edd_mailchimp_signup" type="checkbox">
    			<label for="edd_mailchimp_signup">Sign up for the COMPANY NAME newsletter. <small>(Your email will be used to send you our updates. You can unsubscribe at any time using the link in our emails. For more details, review our <a href="#">privacy policy</a>. We use MailChimp as our marketing automation platform. By checking this box, you acknowledge that your email address will be transferred to MailChimp for processing in accordance with their <a href="https://mailchimp.com/legal/privacy/" rel="noopener" target="_blank">Privacy Policy</a> and <a href="https://mailchimp.com/legal/terms/" rel="noopener" target="_blank">Terms</a>.)</small>
    			</label>
    		</p>
    	</fieldset>
    	<?php
    	echo ob_get_clean();
    }
    
  2. The second piece runs when a customer clicks the final “Purchase” button on the Checkout page. This will check if a customer checked the sign up checkbox. If the customer did check the sign up checkbox, then this will call the function to subscribe them to your MailChimp email list.

    /**
     * Check if a customer needs to be subscribed to MailChimp at checkout
     */
    add_action( 'edd_checkout_before_gateway', function ($posted, $user_info, $valid_data) {
    	if( isset( $posted['isa_edd_mailchimp_signup'] ) ) {
    		$subscribe = isa_subscribe_mailchimp( sanitize_email( $_POST['EMAIL'] ) );
    	}
    }, 10, 3 );
    
  3. This third function is what subscribes the customer’s email address to your MailChimp email list. You must replace 0123456789 with your own MailChimp audience ID on line 9.

    Also, note that this example subscribes them automatically and immediately, without double opt-in. This is because I don’t currently like double opt-in, and it’s not actually required by law in my case (any emails you send out with MailChimp will have an “unsubscribe” button anyway, which mitigates issues in case someone is actually subscribed against their wishes). If you want to enable double opt-in, then simply change subscribed on line 16 to pending. Double opt-in means that they will not be subscribed until they click confirm in a confirmation email that will be sent out to them.

    /**
     * Subscribe an email address to a Mailchimp list
     * @return object Mailchimp API response object
     */
    function isa_subscribe_mailchimp( $email ) {
    
    	// Configure --------------------------------------
    
    	$list_id = '0123456789';// YOUR MAILCHIMP AUDIENCE ID
    	
    	// STOP Configuring -------------------------------
    	
    	$url = 'lists/' . $list_id . '/members/';
    	$body = array(
    		'email_address' => $email,
    		'status' => 'subscribed'
    	);
    	$response = isa_mailchimp_api_request( $url, 'POST', $body );
    	return $response;
    }
    
  4. This final function is a generic function to make any type of request to the MailChimp API version 3.

    This last function is the same that I use in some of my other MailChimp tutorials (here, also here, and here.). So if you’re already using that code, you don’t have to add this final function again.

    You must replace YOUR_API_KEY with your own MailChimp API key on line 15.

    /**
     * Make a request to MailChimp API v3
     *
     * @param string $endpoint The MailChimp API endpoint for this request WITHOUT starting slash
     * @param string $type Type of request, whether GET, POST, PATCH, PUT, or DELETE
     * @param array $body The request body parameters
     *
     * @return $response object The response object
     * 
     */
    function isa_mailchimp_api_request( $endpoint, $type = 'POST', $body = '' ) {
     
        // Configure --------------------------------------
     
        $api_key = 'YOUR_API_KEY';
     
        // STOP Configuring -------------------------------
     
        $core_api_endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
        list(, $datacenter) = explode( '-', $api_key );
        $core_api_endpoint = str_replace( '<dc>', $datacenter, $core_api_endpoint );
     
        $url = $core_api_endpoint . $endpoint;
     
        $request_args = array(
            'method'      => $type,
            'timeout'     => 20,
            'headers'     => array(
                'Content-Type' => 'application/json',
                'Authorization' => 'apikey ' . $api_key
            )
        );
     
        if ( $body ) {
            $request_args['body'] = json_encode( $body );
        }
     
        $request = wp_remote_post( $url, $request_args );
        $response = is_wp_error( $request ) ? false : json_decode( wp_remote_retrieve_body( $request ) );
     
        return $response;
    }
    

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]