Jump to content

Custom hook to unsubscribe customer from MailChimp


5iveleaf

Recommended Posts

I've got this working as a standalone PHP file, but for some reason when I set it up as a custom hook in WHMCS, it no longer works. Here is the content of my hook_mailchimp_unsubscribe.php file which I've placed in /includes/hooks.

 

I thought perhaps it didn't like the email variable, but I've tried hard-coding this with a known email addresses as well which also didn't work. Any ideas what I might be doing wrong here? X's are obviously for obfuscation of actual MailChimp account information.

 


add_hook('AfterShoppingCartCheckout', 1, function ($vars) {
// email variable from WHMCS
   //$email = $vars['clientdetails']['email'];

// Mailchimp call to unsubscribe user based on email
$user  		= 'XXXXXXX';
   $apiKey 	= 'XXXXXXX-us12';
$auth		= base64_encode( 'user:' . $apiKey);
   $listId 	= 'XXXXXXXXXX';
   $memberId 	= md5(strtolower($email));
   $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
   $url 		= 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' .$memberId;

   $json = json_encode(
   	array(
        'status'        => 'unsubscribed' // "subscribed","unsubscribed","cleaned","pending"
	)
);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));

   $result = curl_exec($ch);
   $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);

return $httpCode;

});

 

- - - Updated - - -

 

Oh, and in my actual code the

$email = $vars['clientdetails']['email'];

is not commented out.

 

- - - Updated - - -

 

I've also tried setting the priority to 2 in case there was a conflict of hooks. No dice there either.

Link to comment
Share on other sites

Also tried using the ClientAdd hook instead of AfterShoppingCartCheckout along with

$email = $vars['email'];

Still not working. Is there an issue with running JSON calls within custom hooks? I should probably also mention we're running v 5.3.14 (I know, ancient).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated