Page 1 of 1

mailchimp to s2member deletion

PostPosted: June 24th, 2011, 8:52 pm
by Leinad
if you check

2) Wordpress admin - S2Member - API / List Servers – Automate Un-Subscribe/Opt-Outs

you can see there's an option named "process list removals automatically"

This options can control the sync from s2member to mailchimp. But I can't see options the other way around.

Say me, Daniel create an account in my site, it creates an s2member account and at the same time it adds me to mailchimp.
When the site sends a mail from mailchimp to all users, I'll get a message...and in the mail there's an "unsubscribe" link. When I click this it will delete me from the mailchimp list, but not my s2member user.

Is there ANY way to acomplish this? (delete my s2member account when I click the unsubscribe link)

thanks :)

Re: mailchimp to s2member deletion

PostPosted: June 24th, 2011, 10:20 pm
by BobTabor
In short: I'm 99% sure s2Member doesn't support this today. (I hope I'm wrong ...)

**It IS possible** -- and if you needed this functionality, you may have to pay someone on Elance to code it. They will need this as a reference:

http://apidocs.mailchimp.com/webhooks/

The API supports a "call back" notification when events (like unsubscribes) occur, allowing you to do the look-up and removal. But there are challenges ... keeping user information in sync across two systems would be quite a task. I've done it, and it sucks. Having said that, if you figure out, I would be interested in it, too! Good luck!

Re: mailchimp to s2member deletion

PostPosted: June 25th, 2011, 11:32 am
by Leinad
ok, I just needed to know if it was possible, I'm a PHP coder but mailchimp is somewhat new to me, and I needed a pointer in the right direction.

Well, I'll try, then I'll let you know :P

Re: mailchimp to s2member deletion

PostPosted: July 3rd, 2011, 4:55 pm
by Leinad
I was able to make it work...
this is the webhook file (which is called everytime a user unsubscribes in Mailchimp

Code: Select all
<?php

include("wp-config.php");

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);

foreach(
$_POST as $key => $value)
{
    if(is_array($value))
    {
        foreach($value as $keyValue => $val)
        {
            if($keyValue == "email")
            {
                $email = $val;
            }
        }
    }
}

$sql = "DELETE FROM wp_users  WHERE user_email = '$email'";
$query = mysql_query($sql);

?>

Re: mailchimp to s2member deletion

PostPosted: July 3rd, 2011, 5:09 pm
by BobTabor
AWESOME! Thanks for posting!

... and this would be a good code example for other notifications like 'email updates'.

Re: mailchimp to s2member deletion

PostPosted: July 3rd, 2011, 8:38 pm
by Cristián Lávaque
Thanks a lot for sharing that, Leinad. :)

Re: mailchimp to s2member deletion

PostPosted: July 4th, 2011, 12:42 pm
by Jason Caldwell
Thanks for sharing this. I'll see what we can do to support this in a future release of s2Member.

Re: mailchimp to s2member deletion

PostPosted: July 5th, 2011, 4:52 pm
by Leinad
I have finished the mailchimp to s2member integration.

I needed to create 2 different webhooks. Heres the code.

Code: Select all
<?php

//this one only updates the email, you need to create a webhook that only triggers on "mail changed"
include("wp-config.php");

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);

$stringData .= $sql = "UPDATE wp_users SET user_email = '".$_POST["data"]["new_email"]."' WHERE user_email = '".$_POST["data"]["old_email"]."'";
$query = mysql_query($sql);

?>


and
Code: Select all
<?php

//this one is more tricky hehe, it updates all the data, but as I'm using several custom fields the code added up! Is the same principle, though.

include("wp-config.php");

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);

//select user with the email that matches the notice from mailchimp
$sql = "SELECT * FROM wp_users WHERE user_email = '".$_POST["data"]["email"]."'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);

//update first name
$sql = "SELECT umeta_id FROM wp_usermeta WHERE user_id = ".$row["ID"]." AND meta_key = 'first_name'";
$query = mysql_query($sql);
$umetaId = mysql_result($query, 0);

$stringData .= $sql = "UPDATE wp_usermeta 
                SET 
                    meta_value = '"
.$_POST["data"]["merges"]["MERGE1"]."' 
                WHERE umeta_id = '"
.$umetaId."'";
$query = mysql_query($sql);

//update last name
$sql = "SELECT umeta_id FROM wp_usermeta WHERE user_id = ".$row["ID"]." AND meta_key = 'last_name'";
$query = mysql_query($sql);
$umetaId = mysql_result($query, 0);

$stringData .= $sql = "UPDATE wp_usermeta 
                SET 
                    meta_value = '"
.$_POST["data"]["merges"]["MERGE2"]."' 
                WHERE umeta_id = '"
.$umetaId."'";
$query = mysql_query($sql);

//select the metadata (in my case I'm using a lot of merge fields)
$sql = "SELECT * FROM wp_usermeta WHERE user_id = ".$row["ID"]." AND meta_key = 'wp_s2member_custom_fields'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);

$data["merge3"] = $_POST["data"]["merges"]["MERGE3"];
$data["merge4"] = $_POST["data"]["merges"]["MERGE4"];
$data["merge10"] = $_POST["data"]["merges"]["MERGE10"];
$data["merge5"] = $_POST["data"]["merges"]["MMERGE5"];
$data["merge6"] = $_POST["data"]["merges"]["MMERGE6"];
$data["merge7"] = $_POST["data"]["merges"]["MMERGE7"];
$data["merge8"] = $_POST["data"]["merges"]["MMERGE8"];
$data["merge9"] = $_POST["data"]["merges"]["MERGE9"];
$data["merge13"] = $_POST["data"]["merges"]["MMERGE13"];

$data = serialize($data);

//update that particular meta row
$stringData .= $sql = "UPDATE wp_usermeta 
                SET 
                    meta_value = '"
.$data."' 
                WHERE umeta_id = '"
.$row["umeta_id"]."'";
$query = mysql_query($sql);

?>


I'm still missing the integration from s2member to mailchimp. Can you point me in the right direction? I really don't want to search through the entire code :(