Jump to content

Famous MailChimp Integration without module


mattprestashop

Recommended Posts

Hi, my name is Matt.


 


As everybody know, the newsletter modules of prestashop are not very useful. You cannot see how much subscribers you have, cannot get noticed on new subscriptions etc.. In fact you cannot manage them. I found a free mailchimp module that doesn't really do the the job because first he don't update my subscribers on my mailchimp account and second the module don't work everywhere on the shop where there is a newsletter. Example on user registration its still the shop newsletter subscription.


 


SO, I decided to code my own, really quick, short and sweet. I wish to share it with you and by the same time i'm stuck to the new user registration newsletter subscription. I'll first show you what I did and after that we will talk about where I'm stuck. First I share with you my working code for block_newsletter, and in the second part I need developpers help.


 


I know this is probably not the best way to code it, but its working and I don't really have time to code something better for now I just want to open my shop as fast as possible. I'll modify the code better after. But for now this is 100% working and very well with multiple languages for the shop. My shop will be in FRENCH & ENGLISH. This mean in my MailChimp account i have 2 lists. One for french subscribers and the other for english subscribers.  


 


What I did for now: block_newsletter


This is the module that is normally in the header or footer. This mods allow you to keep all the existing module actions, but also add the user to your MailChimp list, depending of the shop language. I just started coding it so for now the user will still receive the prestashop subscription mail + the subscription mail from mailchimp. You will have to disable it yourself because I didn't did it right now.


 


Here is the code: /modules/blocknewsletter/blocknewsletter.php


 


At the line 287, you will find this:



protected function registerUser($email)
{
$sql = 'UPDATE '._DB_PREFIX_.'customer
SET `newsletter` = 1, newsletter_date_add = NOW(), `ip_registration_newsletter` = \''.pSQL(Tools::getRemoteAddr()).'\'
WHERE `email` = \''.pSQL($email).'\'
AND id_shop = '.$this->context->shop->id;
Then you add:
//mettre code php perso avant le return sinon ca redirect à la page daccueil avantque le code perso soit éxécuté
$iso_code = $this->context->language->iso_code;
if ($iso_code == "fr"){
header('Location: http://terrafalkon.us3.list-manage.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=e476a7ae&EMAIL='.$email.'&debug=user');
}elseif ($iso_code == "en"){
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=user');
}else{
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=user');
}

Here is the final registerUser function:



protected function registerUser($email)
{
$sql = 'UPDATE '._DB_PREFIX_.'customer
SET `newsletter` = 1, newsletter_date_add = NOW(), `ip_registration_newsletter` = \''.pSQL(Tools::getRemoteAddr()).'\'
WHERE `email` = \''.pSQL($email).'\'
AND id_shop = '.$this->context->shop->id;

//mettre code php perso avant le return sinon ca redirect à la page daccueil avantque le code perso soit éxécuté
$iso_code = $this->context->language->iso_code;
if ($iso_code == "fr"){
header('Location: http://terrafalkon.us3.list-manage.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=e476a7ae&EMAIL='.$email.'&debug=user');
}elseif ($iso_code == "en"){
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=user');
}else{
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=user');
}

//écrit dans la table ps_newsletter
return Db::getInstance()->execute($sql);
}

You must do the same thing for the registerGuest Function. Find:



protected function registerGuest($email, $active = true)
{
$sql = 'INSERT INTO '._DB_PREFIX_.'newsletter (id_shop, id_shop_group, email, newsletter_date_add, ip_registration_newsletter, http_referer, active)
VALUES
('.$this->context->shop->id.',
'.$this->context->shop->id_shop_group.',
\''.pSQL($email).'\',
NOW(),
\''.pSQL(Tools::getRemoteAddr()).'\',
(
SELECT c.http_referer
FROM '._DB_PREFIX_.'connections c
WHERE c.id_guest = '.(int)$this->context->customer->id.'
ORDER BY c.date_add DESC LIMIT 1
),
'.(int)$active.'
)';

Then add:



//mettre code php perso avant le return sinon ca redirect à la page daccueil avantque le code perso soit éxécuté
$iso_code = $this->context->language->iso_code;
if ($iso_code == "fr"){
header('Location: http://terrafalkon.us3.list-manage.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=e476a7ae&EMAIL='.$email.'&debug=guest');
}elseif ($iso_code == "en"){
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=guest');
}else{
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=guest');
}

Here is the final version of registerGuest function:



protected function registerGuest($email, $active = true)
{
$sql = 'INSERT INTO '._DB_PREFIX_.'newsletter (id_shop, id_shop_group, email, newsletter_date_add, ip_registration_newsletter, http_referer, active)
VALUES
('.$this->context->shop->id.',
'.$this->context->shop->id_shop_group.',
\''.pSQL($email).'\',
NOW(),
\''.pSQL(Tools::getRemoteAddr()).'\',
(
SELECT c.http_referer
FROM '._DB_PREFIX_.'connections c
WHERE c.id_guest = '.(int)$this->context->customer->id.'
ORDER BY c.date_add DESC LIMIT 1
),
'.(int)$active.'
)';

//mettre code php perso avant le return sinon ca redirect à la page daccueil avantque le code perso soit éxécuté
$iso_code = $this->context->language->iso_code;
if ($iso_code == "fr"){
header('Location: http://terrafalkon.us3.list-manage.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=e476a7ae&EMAIL='.$email.'&debug=guest');
}elseif ($iso_code == "en"){
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=guest');
}else{
header('Location: http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'&debug=guest');
}
//header('Location: http://www.'.$iso_code.'.com');

//écrit dans la table ps_newsletter
return Db::getInstance()->execute($sql);
}

IMPORTANT:


- Don't try my current mailchimp link because I removed some letters/numbers to not get my mailchimp lists spammed. You must replace them by your own mailchimp list links.


- Don't forget to add the $email at the end, very important. This will take the email entered in the newsletter form when a user subscribe. http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c3595586ec3a3f4190b7304&id=bc76babb&EMAIL='.$email.'


- The code must be before the return Db::getInstance because this line will execute the db code above my code and the added code will not be considered. I know  I can make a function and call it instead of all this code but as I said this is just for a quick work right now.


 


How it works:


Simple but work, just make a short redirect to your mailchimp subscription link and then execute the code as normal. You still get all the messages (successful / error / already registered etc..)


 


Thats it!! Next weekend because now I'm don't have the time, i'll continue it and I will disable the subscription mail that is sent by default. I only want the mailchimp mail. However i'll keep the default database addition for the new users, can always be useful.


 


 


Now, second part! I NEED HELP FROM DEVELOPERS HERE


block_newsletter working, I need to get all the new customers registration checkboxes working but thats not as simple as it appear.. I'm not bad in PHP/SQL coding (exemple casinosno1.com is a website I coded entirely and is fully automated with an admin page). But TPL file with routing and all of that, thats new for me. I need to learn how all of this is coded and seriously I don't have really the time for now. This is why now I need help.


 


I think the file we are interested in are:


/controllers/front/AuthController.php


/themes/yourTheme/Authentification.tpl


 


I cannot tell you the exact line number because I added code for tests, but arround #360 you will find this function:



protected function processCustomerNewsletter(&$customer)
{
if (Tools::getValue('newsletter'))
{
$customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
$customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));

if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
if ($module_newsletter->active)

$this->callMailChimp();

$module_newsletter->confirmSubscription(Tools::getValue('email'));
}
}

What I did is trying to call a function called $this->callMailChimp();


 


I added The callMailChimp function just above this function. Of course [email protected] is replaced by my email. I can test the link as many time I want. Each time mailchimp will send a subscription verification to my email because I never clicked the confirmation email on the email.



function callMailChimp(){
echo "<script language=\"JavaScript\">
window.location = \"http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c359ee56ec3a3f4190b7304&id=bc76babb&[email protected]&debug=NewUser\";
</script>";
}

Both function together for a better view:



function callMailChimp(){
echo "<script language=\"JavaScript\">
window.location = \"http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c359ee5586ec3a3f4190b7304&id=bc7699babb&[email protected]&debug=user\";
</script>";
}

/**
* Process the newsletter settings and set the customer infos.
*
* @param Customer $customer Reference on the customer Object.
*
* @note At this point, the email has been validated.
*/
protected function processCustomerNewsletter(&$customer)
{
if (Tools::getValue('newsletter'))
{
$customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
$customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));

if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
if ($module_newsletter->active)

$this->callMailChimp();

$module_newsletter->confirmSubscription(Tools::getValue('email'));
}
}

So, with this.. nothing happen! All is working for the normal registration but no MailChimp registration. Maybe the function is not called properly, maybe the function is not called where it should be.. maybe I'm just totally wrong (probably this.. lol)


 


I tried to see on google if there is a way to call an URL with ajax or something else without opening it.. found nothing to work here.


 


I had the idea to add an hidden iframe on the TPL file and then change the window.location for theframe.location but not working. I think the TPL file (authentification.ypl) is not the good file because the form action load exemple the new user page account after a registration.


 


Anyone know what I should do!? Thanks!!! Hope to have some help here.. will be appreciated!


Edited by mattprestashop (see edit history)
  • Like 3
Link to comment
Share on other sites

Hummm… ok.. HELLO!?

 

Cannot believe no one can help me.. cmon. I took the time to make a well presented topic! ;)

 

Seriously.. need help I open the shop in about 3 weeks. I'll try again today I don't want to spend all the time on that newsletter new customers registration. I'm pretty sure thats a little thing. As I'm not very familiar with this code structuration its frustrating for me right now.

 

Thanks.. AGAIN!  :)  :ph34r:

Link to comment
Share on other sites

Thanks for all the "developers" here for your NO help. Very appreciated (big sarcasm). I'm not a developer and I did the job myself. Big thanks again!


 


I finally got it. All is working 100%. I will still share with you the solution.. I seriously asking myself if I keep it for me but this is an OpenSource hun.. so.. lets share! Probably someone will take this, make a module and sell them. lol !! I don't know what I have this morning but I'm not in a good mood! heh


 


Anyways there you go!


This codes have been tested with those ways and 100% work:


guest register cart empty w/o newsletter subscription


guest register cart empty w newsletter subscription


guest register cart busy w/o newsletter subscription


guest register cart busy w newsletter subscription


 


for the newsletetr block its a separate way, see above.


 


Here is the codes:


/controllers/front/AuthController.php


find: protected function processCustomerNewsletter(&$customer)



protected function processCustomerNewsletter(&$customer)
{
if (Tools::getValue('newsletter'))
{
$customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
$customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));

if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
if ($module_newsletter->active)

//modification perso
$iso_code = $this->context->language->iso_code;

//regarde si le panier contient des produits
//sert pour plus bas pour les redirect dans la processSubmitAccount en 5steps ou 1 step
if (count($this->context->cart->getProducts(true)) > 0){
//si pas vide, redirigera au panier
$cartStatus = "1";
$controller = "order";
}else{
//si vide redirigera sur la page du compte
$cartStatus = "0";
$controller = "myAccount";
}

{ header('Location : http://www.terrafalkon.com/mailchimpAdd.php?controller='.$controller.'&multi-shipping='.(int)Tools::getValue('multi-shipping').'&email='.Tools::getValue('email').'&lang='.$iso_code.'&cartStatus='.$cartStatus.''); }

$module_newsletter->confirmSubscription(Tools::getValue('email'));
}
}

Now create a mailchimpAdd.php file and save it to the shop root folder



<?php
$controller = $_GET['controller'];
$multishipping = $_GET['multi-shipping'];
$email = $_GET['email'];
$lang = $_GET['lang'];
$cartStatus = $_GET['cartStatus'];

if ($lang == "fr"){
$mcRedirect = "http://terrafalkon.us3.list-manage.com/subscribe/post?u=c359ee5586ec3a3f41b7304&id=e4a7eeae&EMAIL=$email";
}elseif ($lang == "en"){
$mcRedirect = "http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c359ee5586ec3a3f41b7304&id=b699babb&EMAIL=$email";
}else{
$mcRedirect = "http://terrafalkon.us3.list-manage1.com/subscribe/post?u=c359ee5586ec3a3f41b7304&id=b699babb&EMAIL=$email";
}

if ($cartStatus == "1"){
$backToshop = "index.php?controller=$controller&multi-shipping=$multishipping";
}else{
$backToshop = "index.php?controller=$controller";
}

?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Redirection..</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!-- redirection security if the mailchimp page doesnt load) -->
<meta http-equiv="refresh" content="15000; url=<?php $backToshop; ?>" />
<!-- redirection after the iframe url is loaded -->
<script language="javascript">
function redirectOnComplete(){
window.setTimeout(function () {
        window.location.href = "<?php echo $backToshop; ?>";
    });
}
</script>
</head>

<body>
<iframe onload="redirectOnComplete()" width=0 height=0 marginwidth=0 marginheight=0 frameborder=0 name="mcframe" id="mcframe" src="<?php echo $mcRedirect; ?>"></iframe>
</body>
</html>

The redirection will be executed only if the website into the iframe have been loaded completely. If the MailChimp page don't want to load, I added a security in the meta. The page will be redirected after 15s.


 


Thats it.. thats the only way right now I found. Passing variables thru an other page and then redirect to the proper page.


 


Don't forget to change the $mcRedirect by YOURS, and don't test the codes as it right now because I modified my $mcRedirect to not get spammed by people here.


 


you can try it on my website as soon as I open it.


 


Cheers!


 


 


Oh last words, if someone make a module with that.. hope you will be enough honest to let me know. And sorry for the french comment, sometimes I wrote my php comments in french and sometimes in english.. lol


Edited by mattprestashop (see edit history)
  • Like 6
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Hey thank you!

 

This looks great... I will definitely put it to use. I'll update with any issues.

 

How has it been going for you and your site? I apologize that no one replied on this. But here I am :).

Thanks.

 

The site is not yet up because i'm still in conception & development for my products. However, I tested the codes and work 300%.

 

:)

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hi Matt

 

though I had no time to test your solution and am no coder I want to truly thank you for committing such a great solution for the public.

Moreover, I want to appreciate that you took your time to explain things well and to open such an extensive thread!

 

I 'd thought that there is a lack of community work in our German forum but well...I wonder to see the same here. But may be fact is that users as me are constantly asking stuff :) and only the hand full of nice and expertlike moderators as Vekia for example are responding in a timely and welcome manner.

 

So, again, please do not delete your lines here and continue contributing.

Link to comment
Share on other sites

  • 1 month later...

Hello Matt,

 

I found your topic searching for a good  solution that i can control easily. I really appreciate your efforts and the time you spend to put things down and complete this task. I didnt test it yet but I ll try to find some time and check it on my testing enviroment. 

I also hope that some good programmers here will help you to debug and test it...Maybe I ll do as well when I ll be free. I ll let you know.

 

THANKS  a lot for sharing, Keep on the good work!  ;)

 

Cheers !!! 

Link to comment
Share on other sites

Hi Matt

 

though I had no time to test your solution and am no coder I want to truly thank you for committing such a great solution for the public.

Moreover, I want to appreciate that you took your time to explain things well and to open such an extensive thread!

 

I 'd thought that there is a lack of community work in our German forum but well...I wonder to see the same here. But may be fact is that users as me are constantly asking stuff :) and only the hand full of nice and expertlike moderators as Vekia for example are responding in a timely and welcome manner.

 

So, again, please do not delete your lines here and continue contributing.

Thank you very much. Really appreciated. :) 

Hello Matt,

 

I found your topic searching for a good  solution that i can control easily. I really appreciate your efforts and the time you spend to put things down and complete this task. I didnt test it yet but I ll try to find some time and check it on my testing enviroment. 

I also hope that some good programmers here will help you to debug and test it...Maybe I ll do as well when I ll be free. I ll let you know.

 

THANKS  a lot for sharing, Keep on the good work!  ;)

 

Cheers !!! 

Keep me up to date on that test. Thanks a lot.

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Hello Matt,

 

Thank you for sharing!  I tried both parts in v.1.6.0.9.  The newsletter block works fine!  With the second part though there seem to be some problem.  I edited AuthController.php as suggested and added a new php to handle the Mailchimp redirection but I get a Duplicate headers error (Error code: ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION).  I was wondering if you had the same error in the process.  I tired some debugging but I cannot find anything useful.  I even suspected having a BOM in the file but I checked and there is nothing there..

 

I will re-post if I find anything.

 

Thanks again,

Effie

Link to comment
Share on other sites

Hi Matt

 

For what version of Prestashop is this for?

 

Yes, Newbie asking

 

Thanks

MMmm I dont remember the exact version because its been doin' a long time now I didn't worked on the website because its now close for an indeterminate time, but thats the version before the big PrestaShop update.

 

 

Hello Matt,

 

Thank you for sharing!  I tried both parts in v.1.6.0.9.  The newsletter block works fine!  With the second part though there seem to be some problem.  I edited AuthController.php as suggested and added a new php to handle the Mailchimp redirection but I get a Duplicate headers error (Error code: ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION).  I was wondering if you had the same error in the process.  I tired some debugging but I cannot find anything useful.  I even suspected having a BOM in the file but I checked and there is nothing there..

 

I will re-post if I find anything.

 

Thanks again,

Effie

 

Sorry for that. As mentionned above I made this mods for the previous version, before they make big changes to the design. Unfortunately I cannot help you right now as I closed the website and its been doin' many months I didn't look at the code so... :P

 

Hope you will find, MAYBE someone here will be able to help you but as I saw people here are not very good in helping other people.

 

:) Cheers!

Link to comment
Share on other sites

Thank you Matt for the code snippet.

We are looking to embed this code snippet into our shop.

 

After reading the code I have this in mind.

Maybe it can help someone.

 

For the mailchimpAdd.php shop Url maybe we can use the {$base_dir} Smarty Variables

{$base_dir}/mailchimpAdd.php?controller=...........

 

I'm just a PrestaShop Newbie at learning
 

Link to comment
Share on other sites

  • 11 months later...

Hi there!

Thanks matt! Your snippet is amazing!

First of all sorry for my english...

The registration to the mailchimp list by the newsletter block it works, but unfortunately if a user creates a new account in the e-commerce and chacks the "Subscribe to newsletter" box, the e-mail is not imported in the mailchimp list.

How can we fix this issue?

Thanks in advance

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...