Events Made Easy Forums How do I … MailChimp Newsletter Subscription Option on RSVP form?

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #53933
    Anonymous
    Inactive

    Hi,

    has anybody successfully added an option to subscribe to a newsletter via MailChimp along with a RSVP form?

    I like to add a checkbox that would allow, while registering for an event, to subscribe to a newsletter via MailChimp.

    Anybody done this already and can give some guidance where to start?

    Many thanks and happy Eatser!
    Cheers
    JK

    #53937
    Franky
    Keymaster

    Use the following action hook in EME for this: eme_insert_rsvp_action
    and then call the mailchimp API (which I must admit: I do not know)

    #54065
    Anonymous
    Inactive

    Thank you Franky for the hint.

    But again as I’m not a programmer I’m struggling to find a solution …

    What I found is a description to the MailChimp API that shows a subscription call I think…

    https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

    #54068
    Franky
    Keymaster

    I’m sorry, but while I do provide all means to do what you want here (with action hook examples), this is functionality not related to Events Made Easy, so you should hire an external party to do it for you if you can’t do this. You could hire me, but I’m charging by the hour for these things. Or you could try to ask mailchimp for the support.
    Start from this (do it for a specific event_id or for all, and change MY_FIELD_NAME to the field you want to check.

    
    add_action('eme_insert_rsvp_action', 'my_eme_discount_function',20,1);
    function my_eme_discount_function($booking) {
       global $wpdb;
       $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME;
       $where = array();
       $fields = array();
    
       $booking_id = $booking['booking_id'];
       $event_id = $booking['event_id'];
       $event = eme_get_event($event_id);
       $person = eme_get_person($booking['person_id']);
    
       if ($event_id == 5) {        /* put in the event_id that needs processing, or leave out this line to process all events */
            $answers = eme_get_answers($booking_id);
            $my_answer = "";
            foreach ($answers as $answer) {
                if ($answer['field_name'] == "MY_FIELD_NAME") {
                     $my_answer = $answer['answer'];
                }
            }
            if ($my_answer == "my wanted text") {
                    // do mailchimp thing here, based on the info in $booking, $event and/or $person
            }
       }
       return;
    }
    
    #54108
    Anonymous
    Inactive

    Hello Franky,

    I absolutely understand!

    May I just ask two questions to not run into a death end please?

    1) Am I on a good way with this?

    add_action('eme_insert_rsvp_action', 'my_eme_mailchimpsubscription',20,1);
    
    /**
     * Custom function to subscribe to the newsletter
     */
    
    function my_eme_mailchimpsubscription($booking) {
    	global $wpdb;
    	$bookings_table = $wpdb->prefix.BOOKINGS_TBNAME;
    
    	$event_id = $booking['event_id'];
    	$booking_id = $booking['booking_id'];
    	$answers = eme_get_answers($booking_id);
      	$newsletter = "";
     
        foreach ($answers as $answer) {
           if ($answer['field_name'] == "Newsletter") {
                $newsletter = $answer['answer'];
           }
        }
      
        if ($newsletter == "Subscribe") {
    
    	  //do subscription
       
        }
       
    	return;
    }

    2) Can I have multiple actions?

    add_action(’eme_insert_rsvp_action’, ‘my_eme_coupons’,20,1);
    add_action(’eme_insert_rsvp_action’, ‘my_eme_mailchimpsubscription’,20,1);

    Do I have to modify something?

    Many thanks in advance.

    Cheers
    JK

    #54109
    Franky
    Keymaster

    Concerning 1: yes, seems to be ok
    Concerning 2: good question … according to what I find on the net, wordpress has no issues with this. Although you should use the priority option correctly to define which one to run first.
    Or: do everything in the same action hook, and call other functions from there (seems cleaner to me).
    Edit: see also http://wordpress.stackexchange.com/questions/6688/how-many-times-can-i-hook-into-the-same-action (the last answer)

    #54110
    Anonymous
    Inactive

    Hi Franky,

    I’m sorry just another question…

    $person = eme_get_person($booking['person_id']);

    How do I get the Firstname, Lastname and Emailaddress out of $person into a variable?

    Many thanks for any direction.

    Cheers
    JK

    #54111
    Anonymous
    Inactive

    I was trying to do this:

    
        $person = eme_get_person($booking['person_id']); 
        $subscribeemail = $person.email;
        $subscribelname = $person.lastname;
        $subscribefname = $person.firstname;
    
    #54112
    Anonymous
    Inactive

    But I think it has to be:

        $person = eme_get_person($booking['person_id']); 
        $subscribeemail = $person['email'];
        $subscribelname = $person['lastname'];
        $subscribefname = $person['firstname'];
    #54113
    Franky
    Keymaster

    Indeed, $person is a plain php array.

    #54114
    Anonymous
    Inactive

    Cool 😀

    I now have installed a small api wrapper (https://github.com/drewm/mailchimp-api/) for MailChimp.

    And adjusted my code and decided to do as you said adding all into a single action…

    /**
     * Add a hook for the Events Made Easy system to allow for coupon codes
     */
    add_action('eme_insert_rsvp_action', 'my_eme_custom_stuff',20,1);
    
    /**
     * Custom function to calculate coupon code discounts for events
     */
    
    function my_eme_custom_stuff($booking) {
    
        include('/myurl/wp-content/uploads/MailChimp.php');
      
        global $wpdb;
    	$bookings_table = $wpdb->prefix.BOOKINGS_TBNAME;
    	$discount = 10;
        $dealprice = 89; 
    	$where = array();
    	$fields = array();
    
      
    	// Grab the coupon code from the extra answers
    	$event_id = $booking['event_id'];
    	$booking_id = $booking['booking_id'];
        $person = eme_get_person($booking['person_id']); 
        $answers = eme_get_answers($booking_id);
        $newsletter = "";
    	$coupon = "";
      
        foreach ($answers as $answer) {
           if ($answer['field_name'] == "Coupon") {
                 $coupon = $answer['answer'];
              } else if ($answer['field_name'] == "Newsletter") {
        		 $newsletter = $answer['answer'];
     	  }
        }
      
      
    	// As long as the coupon code isn't empty, look for matches
        // make the coupon code CAPITALS
        $coupon = strtoupper($coupon);
    	if ($coupon == "COUNPON10") {
    		// If coupon code used, apply the appropriate price change
    		$price = $booking['booking_price'] - ( $booking['booking_price'] * ($discount / 100));
    			
    		$fields['booking_price'] = $price;
    		$where['booking_id'] = $booking['booking_id'];
    		$wpdb->update($bookings_table, $fields, $where);
    	} else if ($coupon == "NEWSLETTER2015" || $coupon == "CAMPAIN2015") {
    		// If coupon code used, apply the appropriate price change
    		$price = $dealprice;
    			
    		$fields['booking_price'] = $price;
    		$where['booking_id'] = $booking['booking_id'];
    		$wpdb->update($bookings_table, $fields, $where);
    	}
     
      
      // Subscription to MailChimp
       
       	if ($newsletter == "Ich möchte den Sensorgrafie Newsletter erhalten.") {
    
            
            $MailChimp = new \Drewm\MailChimp('MYAPIREF-us10');
      
            // Grab if subscription is asked from the extra answers
          
            $result = $MailChimp->call('lists/subscribe', array(
                    'id'                => 'MYLISTID',
                    'email'             => array('email'=> $person['email']),
                    'merge_vars'        => array('FNAME'=> $person['firstname'], 'LNAME'=> $person['lastname']),
                    'double_optin'      => false,
                    'update_existing'   => true,
                    'replace_interests' => false,
                    'send_welcome'      => true,
                ));  
    
        }  
    	
    
    	return;
    }

    Just to contribute if anybody likes to add the service as well 😉

    #54115
    Franky
    Keymaster

    Thanks for this mailchimp API example!
    Small question: is that really the content of your field: “Ich möchte den Sensorgrafie Newsletter erhalten.” ? Maybe just a checkbox with values 0/1 would be easier to check 🙂

    #54116
    Anonymous
    Inactive

    Yes, it is, just forgot to delete this in the example…

    #54117
    Anonymous
    Inactive

    I use the field value to be displayed at my form that why the response is also so long. Maybe I just don’t know how to do it better or different 🙂

Viewing 14 posts - 1 through 14 (of 14 total)
  • The forum ‘How do I …’ is closed to new topics and replies.
Scroll to Top